JSONObject quotesObject = new JSONObject(responseStr);
Log.e("leb","4");
//get it as array
JSONArray projects = quotesObject.getJSONArray("project");
我从服务器上获得了Json。 这些是我的错误信息:
12-19 11:56:17.314: W/System.err(456): org.json.JSONException: Value {"category":"1","Body":"eeee","Price":"110.0","ID":"1","NowPrice":"10.0","idRef":"1","user":"1","Title":"I"} at project of type org.json.JSONObject cannot be converted to JSONArray
12-19 11:56:17.374: W/System.err(456): at org.json.JSON.typeMismatch(JSON.java:96)
12-19 11:56:17.374: W/System.err(456): at org.json.JSONObject.getJSONArray(JSONObject.java:548)
12-19 11:56:17.413: W/System.err(456): at com.supinfo.sup.rest.ProjectRestDao$RestTaskGetAll.doInBackground(ProjectRestDao.java:197)
12-19 11:56:17.434: W/System.err(456): at com.supinfo.sup.rest.ProjectRestDao$RestTaskGetAll.doInBackground(ProjectRestDao.java:1)
12-19 11:56:17.434: W/System.err(456): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-19 11:56:17.434: W/System.err(456): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-19 11:56:17.434: W/System.err(456): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-19 11:56:17.434: W/System.err(456): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-19 11:56:17.434: W/System.err(456): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-19 11:56:17.434: W/System.err(456): at java.lang.Thread.run(Thread.java:1096)
答案 0 :(得分:0)
两者都是为了不同目的而开发并解决不同的问题,因为对象可以将多个值存储在单个对象中,对于JSONArray,您可以保存多个对象并遍历它们。
如果由于转换原因而遇到问题,那么解析逻辑就会出现问题。正确分析JSON输出然后解析它。
答案 1 :(得分:0)
在您的回复中,您没有任何JSONArray
。它只包含JSONObject
。所以你必须解析它如下:
只需从给定的字符串创建JSONObject
,然后从jsonObject中提取所有值:
//create json object form responce string
JSONObject object = new JSONObject(responseStr);
//get nickname
String category= object.getString("category");
String body=object.getString("Body");
///.... get other value for object
并使用JSONObject.has(String name)进行检查当前json对象是否包含给定名称的映射