0

好的,所以第一个障碍在几分钟内就被清除了,让我们看看这个障碍是怎么做的。

正如我在日志中看到的那样,该代码正在从网站上提取 JSON 数据,但现在 JSONParser 失败了。我认为问题在于 JSONObject 与 JSONArray,但我无法弄清楚。

以下是从 JSON 站点提取的数据,如日志中所示:

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}]}}

这是尝试解析数据的代码:

            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());
        }

这是失败时日志文件中的条目:

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
4

2 回答 2

3

您不能在不指定 Object 或 Array 的情况下使用 Java API 解析 JSON — JSON 文档都可以。由于您在日志中的数据显示了一个对象,因此您需要在第二行请求一个 JSONObject。

于 2012-09-10T15:31:28.790 回答
1

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.

于 2012-09-10T15:45:55.177 回答