我目前在转换以下类型的 JSON 数据时遇到问题。我正在尝试在 Android 中显示书名并显示章节及其内容。但是,我收到了 JSON.typeMismatch 错误。我认为代码不正确,但找不到问题。我在下面添加了我的示例代码。
一些示例或提示会有所帮助。我很想听听你的意见!
(
{
"abbrev" : "abbrev"
"book" : "name"
"chapters":
(
{"1": {"1": "...", "2": "..."}}, {"2": {"1": "...", "2": "..."}},
{"2": {"1": "...", "2": "..."}}, {"2": {"1": "...", "2": "..."}},
{"3": {"1": "...", "2": "..."}}, {"2": {"1": "...", "2": "..."}}
)
}
)
这是我尝试过的一些代码。我想要做的就是获取字符串数据。
mCreateJson = new CreateJsonData(this, "sample.json");
JSONObject jsonObj = mCreateJson.getJsonObject(); ⇦ // Contains above json objects
JSONArray jsonArray;
try {
for (int i = 0; i < jsonObj.length(); i++) {
jsonArray = jsonObj.getJSONArray("chapters");
android.util.Log.d("Test", jsonArray.toString());
for(int a = 0; a < jsonArray.length(); a++ ) {
}
}
} catch (JSONException e) {
e.printStackTrace();
}
这是你的json
{
"abbrev" : "abbrev"
"book" : "name"
"chapters":
[
{"1": {"1": "...", "2": "..."}}, {"2": {"1": "...", "2": "..."}},
{"2": {"1": "...", "2": "..."}}, {"2": {"1": "...", "2": "..."}},
{"3": {"1": "...", "2": "..."}}, {"2": {"1": "...", "2": "..."}}
]
}
并解析
mCreateJson = new CreateJsonData(this, "sample.json");
JSONObject jsonObj = mCreateJson.getJsonObject(); ⇦ // Contains above json objects
JSONArray jsonArray;
try {
for (int i = 0; i < jsonObj.length(); i++) {
jsonArray = jsonObj.getJSONArray("chapters");
android.util.Log.d("Test", jsonArray.toString());
for(int a = 0; a < jsonArray.length(); a++ ) {
JSONObject js = jsonArray.getJsonObject(a);
String ss1=js.getString("1");
//and so on as your json
}
}
} catch (JSONException e) {
e.printStackTrace();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句