2013-02-28 88 views
0

如何分析JSONArray里面JSONObject?这是我从服务器获得的JSON响应。解析的JSONObject和JSONArray和修复它们在列表视图

{ 
"searchdata": { 
    "titles": [ 
     "<b>Laptop</b> - Wikipedia, the free encyclopedia", 
     "<b>laptop</b> - definition of <b>laptop</b> by the Free Online Dictionary ..." 
    ], 
    "desc": [ 
     "A <b>laptop</b> computer is a personal computer for mobile use. A <b>laptop</b> has most of the same components as a desktop computer, including a display, a keyboard, a ...", 
     "lap·top (l p t p) n. A portable computer small enough to use on one&apos;s lap. <b>laptop</b> [ˈlæpˌtɒp], <b>laptop</b> computer. n (Electronics &amp; Computer Science/Computer ..." 
    ], 
    "links": [ 
     "http://en.wikipedia.org/wiki/Laptop", 
     "http://www.thefreedictionary.com/laptop" 
    ], 
    "nextpage": "" 
} 
} 

我能够得到JSONObject但如何通过一个拿到JSONArray一个,这样我就可以在listview解决这些问题。

我要显示的每个数组的值在listview等单行....

任何帮助将不胜感激。

+0

您可以使用GSON库进行JSON解析(而不是手动执行)。看看这里的例子:http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html – Piotr 2013-02-28 08:04:44

回答

1

很容易..

你需要修复这样的代码:

//jsonString is your whole JSONString which you have shown above 

JSONObject jObj = new JSONObject(jsonString); 
JSONObject jSearchData = jObj.getJSONObject("searchdata"); 
JSONArray jTitles = jSearchData.getJSONArray("titles"); 
JSONArray jDesc= jSearchData.getJSONArray("desc"); 
JSONArray jLinks= jSearchData.getJSONArray("links"); 
String nextPage = jSearchData.getString("nextpage"); 
//and so on and so forth 

有关读取的数组项,并将其显示成列表视图:

//you can iterate over each item and add it to an ArrayList like this: 

//i am showing you a single one,follow the same process for other arrays: 

ArrayList titlesArray = new ArrayList(); 

for (int i = 0; i < jTitles.length(); i++) { 
String item = jTitles.getString(i); 
titlesArray.add(item); 

} 

Next你让此ArrayList到ListView来源是这样的:

// Get a handle to the list view 
    ListView lv = (ListView) findViewById(R.id.ListView01); 
lv.setAdapter(new ArrayAdapter<string>((Your activity class).this, 
      android.R.layout.simple_list_item_1, titlesArray)); 
+0

嘿NEZAM,我已经添加了阵列中的每个项目,所以现在我怎么能叫那些项目在列表视图中。你能帮助我吗? – Anupam 2013-02-28 09:06:00

+0

只需将其设置为适配器即可。如..到位titlesArray的提供您的阵列,其中在i贴 – Nezam 2013-02-28 09:07:36

+0

http://stackoverflow.com/questions/15131306/fix-jsonresponse-on-listview/15131399#15131399 – Anupam 2013-02-28 09:07:58

0

它会帮助:

JSONArray titles = new jSONObject(jsonstring).getJSONObject("searchdata").getJSONArray("titles"); 

JSONArray desc = new jSONObject(jsonstring).getJSONObject("searchdata").getJSONArray("desc"); 

JSONArray links = new jSONObject(jsonstring).getJSONObject("searchdata").getJSONArray("links"); 
1

想想看,你的顶级JSON将被解析成JSONObject,并且随后可以请求从中得到任何子级别的对象和/或通过方法getJSONObject(name)getJSONArray(name)。您感兴趣的阵列是两个层次深的JSON层次,所以你需要像这样:

String json = ...; 
JSONObject rootObj = new JSONObject(json); 
JSONObject searchObj = rootObj.getJSONObject("searchdata"); 
JSONArray titlesObj = searchObj.getJSONArray("titles"); 
JSONArray descsObj = searchObj.getJSONArray("desc"); 
JSONArray linksObj = searchObj.getJSONArray("links"); 

可以遍历任意阵列本身(使用titles为例):

for(int i = 0; i < titlesObj.length(); i++) { 
    String title = titlesObj.getString(i); 
} 
+0

迭代时,我得到错误说明 - 类型不匹配。 '不能将字符串转换为字符串''title = titlesObj.get(i);'为什么? – Anupam 2013-02-28 07:34:46

+0

解决!谢谢您的回答。 – Anupam 2013-02-28 07:35:33

+0

@Anupam - 方法应该是'getString',而不是'get'。我已经修复了代码示例。另外,不要忘记接受最能帮助你的答案! – Perception 2013-02-28 07:36:51

相关问题