Java代码看起来像下面; 在这里,我想要在活动中显示要显示的字符串所以,任何人都可以帮助我从JSONArray中找到结果字符串
try {
JSONArray productJsonArray = new JSONArray(result);
int count = productJsonArray.length();
if (count != 0) {
for (int i = 0; i < productJsonArray.length(); i++) {
JSONObject productObject = productJsonArray
.getJSONObject(i);
String pID = productObject.getString("ID");
String pTitle = productObject.getString("title");
String pSDesc = productObject.getString("content");
String pImgSrc = productObject
.getJSONArray("featured_image")
.optJSONObject(0).getString("source");
NewsModel model = new NewsModel();
model.setName(pTitle);
model.setDesc(pSDesc);
model.setId(pID);
model.setImageName(pImgSrc);
newsArrayList.add(model);
}
这是JSON Rsult:
[2]
0: {
ID: 216
title: "New Farcry 4 release"
status: "publish"
modified_gmt: "2014-09-24T07:15:18"
meta: {...}-
featured_image: {
ID: 217
title: "Far-Cry-2-1422"
status: "inherit"
type: "attachment"
author: {...}-
parent: 216
date: "2014-09-24T07:15:14"
modified: "2014-09-24T07:15:14"
format: "standard"
source: "http://www.thusya.com/gamecom/wp-content/uploads/2014/09/Far-Cry-2-1422.jpg"
}-
terms: [0]
}-
这里我有例如下面的
09-25 19:06:24.308: W/System.err(12575): at org.json.JSON.typeMismatch(JSON.java:111)
09-25 19:06:24.308: W/System.err(12575): at org.json.JSONObject.<init>(JSONObject.java:159)
09-25 19:06:24.308: W/System.err(12575): at org.json.JSONObject.<init>(JSONObject.java:172)
09-25 19:06:24.318: W/System.err(12575): at com.tdzinfo.gamecom.News$GetNews.onPostExecute(News.java:101)
09-25 19:06:24.318: W/System.err(12575): at com.tdzinfo.gamecom.News$GetNews.onPostExecute(News.java:1)
09-25 19:06:24.318: W/System.err(12575): at android.os.AsyncTask.finish(AsyncTask.java:632)
09-25 19:06:24.318: W/System.err(12575): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-25 19:06:24.318: W/System.err(12575): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
09-25 19:06:24.318: W/System.err(12575): at android.os.Handler.dispatchMessage(Handler.java:102)
09-25 19:06:24.318: W/System.err(12575): at android.os.Looper.loop(Looper.java:157)
09-25 19:06:24.318: W/System.err(12575): at android.app.ActivityThread.main(ActivityThread.java:5335)
09-25 19:06:24.318: W/System.err(12575): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 19:06:24.318: W/System.err(12575): at java.lang.reflect.Method.invoke(Method.java:515)
09-25 19:06:24.318: W/System.err(12575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-25 19:06:24.328: W/System.err(12575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-25 19:06:24.328: W/System.err(12575): at dalvik.system.NativeStart.main(Native Method)
所以我想知道如何处理这个来从这个JSOn结果中获取源名称
答案 0 :(得分:0)
featured_image
不是数组而是对象。
更改行
String pImgSrc = productObject.getJSONArray("featured_image").optJSONObject(0).getString("source");
到
String pImgSrc = productObject.getJSONObject("featured_image").getString("source");