我正在尝试解析this JSON,
{
"listname": "red",
"lists": [
{
"id": "01",
"name": "paw",
"list": [
{
"id": "A",
"name": "pawa",
"bar": "foo"
},
{
"id": "B",
"name": "pawb",
"bar": "foo"
}
]
},
{
"id": "02",
"name": "pew",
"list": [
{
"id": "A",
"name": "pewa",
"bar": "foo"
},
{
"id": "B",
"name": "pewb",
"bar": "foo"
}
]
},
{
"id": "03",
"name": "piw",
"list": [
{
"id": "A",
"name": "piwa",
"bar": "foo"
},
{
"id": "B",
"name": "piwb",
"bar": "foo"
}
]
}
]
}
我把它放在Asset Folder
上我读了它并将它转换为我String
,因为这里一切都很好,问题是当我试图从name
获取lists
时list
的每个项目,并尝试从JSONObject obj = new JSONObject(str);
JSONArray jsonMainArr = obj.getJSONArray("lists"); //first get the lists
for (int i = 0; i < jsonMainArr.length(); i++) {
JSONObject childJSONObject = jsonMainArr.getJSONObject(i);
String name = childJSONObject.getString("name");
Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}
JSONObject obj = new JSONObject(str);
JSONArray jsonMainArr = obj.getJSONArray("list"); //get the list
for (int i = 0; i < jsonMainArr.length(); i++) {
JSONObject childJSONObject = jsonMainArr.getJSONObject(i);
String name = childJSONObject.getString("name");
Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}
获取所有名称我尝试过这样做:
JSON
但它没有显示任何东西......我缺少什么?
这就是我阅读 public static String loadJSONFromAsset(Context ctx, String str) {
String json = null;
try {
InputStream is = ctx.getAssets().open(str);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
import blaze as bz
bdata = bz.Data([(1, 'Alice', 100.9, 100),
(2, 'Bob', 200.6, 200),
(3, 'Charlie', 300.45, 300),
(5, 'Edith', 400, 400)],
fields=['id', 'name', 'revenue', 'profit'])
答案 0 :(得分:2)
因为你的&#34;列表&#34;嵌套在'\w'
(来自&#34;列表&#34;)中,嵌套for循环以检索这组值(JSONobject)
childJSONObject