好的,所以第一个障碍在几分钟内就被清除了,让我们看看这个障碍是怎么做的。
正如我在日志中看到的那样,该代码正在从网站上提取 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