JSON parsing in android: No value for x error

前端 未结 2 1279
春和景丽
春和景丽 2020-12-20 01:12

Here is the code I\'m using inside my AsyncTask

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);    
request.s         


        
相关标签:
2条回答
  • 2020-12-20 01:37

    Your JSON Object contains itself a JSONObject. To acces to your data, you have to do like this:

    vehicle.getJSONObject("GetJSONObjectResult").getString("make");
    
    0 讨论(0)
  • 2020-12-20 01:45

    No value for x error message is pretty common when dealing with JSON. This usually resulted by overlooked code.

    usually, when dong JSON, I try to see the human readable structure first. For that, I usually use JSONViewer.

    In your case, the structure is something like this:

    enter image description here

    You see that make is within another object called GetJSONObjectResult. Therefore, to get it, you must first get the container object first:

    JSONObject vehicle = ((JSONObject)new JSONObject(result)).getJSONObject("GetJSONObjectResult");
    //a more easy to read
    JSONObject container = new JSONObject(result);
    JSONObject vehicle = container.getJSONObject("GetJSONObjectResult");
    

    and finally use the object to get make:

    makeEdit.setText(vehicle.getString("make"));
    plateEdit.setText(vehicle.getString("plate"));
    modelEdit.setText(vehicle.getString("model"));
    yearEdit.setText(vehicle.getString("year"));
    
    0 讨论(0)
提交回复
热议问题