gpt4 book ai didi

java - 解析 JSONObject 和 JSONArray 并在 ListView 中修复它们

转载 作者:行者123 更新时间:2023-12-02 07:14:18 25 4
gpt4 key购买 nike

如何解析 JSONObject 中的 JSONArray?这是我从服务器获得的 JSON 响应。

{
"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的单行中显示每个数组的值,依此类推......

任何帮助将不胜感激。

最佳答案

非常简单..

您需要修复如下代码:

//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

用于获取数组项并将其显示到 ListView 中:

//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);

}

接下来,将此数组列表作为 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));

关于java - 解析 JSONObject 和 JSONArray 并在 ListView 中修复它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15129638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com