如何解析json数据

时间:2014-02-17 09:00:20

标签: android json

我在使用从网上获取JSON数据时非常新,所以我在这里很困惑。如何在android中获取此JSON数据:我在json fromat中收到错误。

喜欢这个日志猫。

02-16 21:31:25.093: W/System.err(22428): org.json.JSONException: Value {"3":{"ID":1172,"title":"dsfsdf"},"9":{"ID":1172,"title":"dsfsdf"},"8":{"ID":1172,"title":"dsfsdf"},"7":{"ID":1172,"title":"dsfsdf"},"6":{"ID":1172,"title":"dsfsdf"},"5":{"ID":1172,"title":"dsfsdf"},"4":{"ID":1172,"title":"dsfsdf"}} at data of type org.json.JSONObject cannot be converted to JSONArray
02-16 21:31:25.133: W/System.err(22428):    at org.json.JSON.typeMismatch(JSON.java:100)
02-16 21:31:25.138: W/System.err(22428):    at org.json.JSONObject.getJSONArray(JSONObject.java:548)
02-16 21:31:25.138: W/System.err(22428):    at auto.life.dashboard.LeftSideMenuController$MyPickAsyncTask.doInBackground(LeftSideMenuController.java:1807)
02-16 21:31:25.138: W/System.err(22428):    at auto.life.dashboard.LeftSideMenuController$MyPickAsyncTask.doInBackground(LeftSideMenuController.java:1)
02-16 21:31:25.138: W/System.err(22428):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-16 21:31:25.138: W/System.err(22428):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-16 21:31:25.138: W/System.err(22428):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-16 21:31:25.138: W/System.err(22428):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-16 21:31:25.138: W/System.err(22428):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-16 21:31:25.138: W/System.err(22428):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-16 21:31:25.138: W/System.err(22428):    at java.lang.Thread.run(Thread.java:856)
02-16 21:36:26.873: W/IInputConnectionWrapper(22428): getSelectedText on inactive InputConnection
02-16 21:36:26.873: W/IInputConnectionWrapper(22428): setComposingText on inactive InputConnection
02-16 21:36:26.873: W/IInputConnectionWrapper(22428): getExtractedText on inactive InputConnection

我有像这样的Json数据。

{
    "get": [],
    "post": {
        "_event": "mydatajson",
         "user_id": "12"
    },
    "data": {
        "3": {
            "ID": 1172,
            "title": "dsfsdf"
        },
        "4": {
            "ID": 1172,
            "title": "dsfsdf"
        },
        "5": {
            "ID": 1172,
            "title": "dsfsdf"
        },
        "6": {
            "ID": 1172,
            "title": "dsfsdf"
        },
        "7": {
            "ID": 1172,
            "title": "dsfsdf"
        },
        "8": {
            "ID": 1172,
            "title": "dsfsdf"
        },
        "9": {
            "ID": 1172,
            "title": "dsfsdf"
        }
    }
}

我尝试从java获取数据:

List<NameValuePair> mypickDetails=new ArrayList<NameValuePair>();
        mypickDetails.add(new BasicNameValuePair("_event", event));

        mypickDetails.add(new BasicNameValuePair("user_id", userID));


        HttpClient hc=new DefaultHttpClient();

        HttpPost hp=new HttpPost(HttpPath.url);

        try {

            hp.setEntity(new UrlEncodedFormEntity(mypickDetails));

            // Execute HTTP Post Request
            HttpResponse response=hc.execute(hp);


            String result = EntityUtils.toString(response.getEntity());

           String jsontext = new String(result);


                //take object according to json format result                        

                JSONObject entries = new JSONObject(jsontext);

                JSONArray postListArray = entries.getJSONArray("data");

                for (int i = 0; i < postListArray.length(); i++) {
                    Log.d("Geting Value---", "+----"+postListArray.length());
                    JSONObject postListObj = postListArray.getJSONObject(i);

                    String myPickName = postListObj.getString("title");


                    DashboardVariables.mypickname.add(myPickName);




            }

谢谢你提前。

3 个答案:

答案 0 :(得分:1)

 "data": {

data不是JSONArray。它是JSONObject

但你有

 JSONArray postListArray = entries.getJSONArray("data"); // wrong

{表示json对象节点

[表示json数组节点

"data": { // json object data
        "3": { // json object 3
            "ID": 1172,  // int 
            "title": "dsfsdf" // string
        }

使用

 JSONObject jb = (JSONObject)entries.getJSONObject("data");   
 JSONObject three = jb.getJSONObject("3");
 int id = three.getInt("ID");
 String title = three.getString("title"); 

答案 1 :(得分:0)

使用

.getJSONObject("data") 

因为数据是JSON对象。 JSON数组以[]

开头

答案 2 :(得分:0)

&#34;数据&#34; JSON中的字段包含对象,而不是数组。错误消息告诉您。

您可能希望访问json.org以更好地理解JSON对象的语法和结构; []用于表示数组。

因此,您需要将其作为对象获取而不是数组:

JSONObject dataObject = entries.getJSONObject("data");

然后,如果您想遍历该对象中的字段/对象,则需要获取键(字段)列表并获取每个对象:

for (String key : dataObject.keys())
{
    JSONObject postListObj = dataObject.getJSONObject(key);
    ...
}