简体   繁体   中英

Android app to parse JSON from website

Ok, so the first hurdle was cleared in just a few minutes, let's see how this one does.

The code is pulling the JSON data from the website fine as I can see in my log, but now the JSONParser is failing. I think the issue is JSONObject versus JSONArray, but I can't figure that out.

Here is the data pulled from the JSON site as seen in the log:

09-10 09:45:00.175: I/log_tag(785): {"stoker":{"sensors":[{"id":"620000116F01CA30","name":"SS2","al":0,"ta":66,"th":75,"tl":65,"tc":66.3,"blower":null},09-10 09:45:00.175: I/log_tag(785): {"id":"E20000116F0CDB30","name":"brskt2","al":0,"ta":203,"th":32,"tl":32,"tc":70.6,"blower":null}],09-10 09:45:00.175: I/log_tag(785): "blowers":[{"id":"37000000119D8B05","name":"party","on":0}]}}

And here is the code trying to parse the data:

            try{
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                Sensor resultRow = new Sensor();
                resultRow.id = json_data.getString("id");
                resultRow.name = json_data.getString("name");
                resultRow.current = json_data.getString("tc");
                resultRow.target = json_data.getString("ta");
                arrayOfWebData.add(resultRow);
            }
        }
        catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

And here is the entry in the log file when it fails:

09-10 09:45:00.314: E/log_tag(785): Error parsing data org.json.JSONException: Value {"stoker":{"sensors":[{"id":"620000116F01CA30","al":0,"tl":65,"tc":66.3,"ta":66,"name":"SS2","blower":null,"th":75},{"id":"E20000116F0CDB30","al":0,"tl":32,"tc":70.6,"ta":203,"name":"brskt2","blower":null,"th":32}],"blowers":[{"id":"37000000119D8B05","on":0,"name":"party"}]}} of type org.json.JSONObject cannot be converted to JSONArray

You can't just parse JSON using the Java API without specifying Object or Array — a JSON document can be either. Since your data in the log shows an Object, you need to ask for a JSONObject on your second line.

So long as the JSON string in your logcat is accurate, there are two things that need to change. First, your result is a JSONObject that contains a JSONArray. You'll have to make an object first and extract the array from that object. Secondly, tc and ta are shown as doubles and integers respectively in the output and would need to be retrieved as such. It'd all look something like this:

    try {
        JSONObject obj = new JSONObject(result);
        JSONObject stoker = obj.getJSONObject("stoker");
        JSONArray jArray = stoker.getJSONArray("sensors");
        for(int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            Sensor resultRow = new Sensor();
            resultRow.id = json_data.getString("id");
            resultRow.name = json_data.getString("name");
            resultRow.current = json_data.getDouble("tc");
            resultRow.target = json_data.getInt("ta");
            arrayOfWebData.add(resultRow);
        }
    }
    catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
    }

Based on the provided JSON string, this should work. For future reference, anything contained within {} is a JSONObject while anything inside of [] is a JSONArray. Any second value in a key/value pair that has quotes around it is likely a string while values without quotes are retrieved as numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM