我在Android and Xively.
我得到JSON data
,如下所示。
{"id":10541167124,"title":"air-conditioner","private":"false","feed":"https://api.xively.com/v2/feeds/10541167124.json","status":"frozen","updated":"2014-08-15T08:23:01.612382Z","created":"2014-08-15T08:17:17.336198Z","creator":"https://xively.com/users/x22819","version":"1.0.0","datastreams":[{"id":"air-aonditioner1","current_value":"25","at":"2014-08-15T08:23:01.521837Z","max_value":"25.0","min_value":"25.0","tags":["air=off","eco=off","freeze_proofing=off","power=on","stop_time=1","temp_set=24","wind_speed=low"],"unit":{"symbol":"C","label":"Celsius"}}],"product_id":"uq8_wTGk4Kdmfmlaa1B-","device_serial":"7AHJ84EAQDEZ"}
我使用以下代码获取tags
数据。
String resultJSON = response.getContent();
try {
JSONObject jsonRoot = new JSONObject(resultJSON);
JSONArray jsonData = jsonRoot.getJSONArray("datastreams");
for(int i=0 ; i < jsonData.length() ; i++)
{
JSONObject jsonOBject = jsonData.getJSONObject(i);
String tags = jsonOBject.getString("tags");
Log.(TAG, "tags = "+ tags + "\n");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tags
的数据如下所示
D/DemoActivity(25686): tags = ["air=off","eco=off","freeze_proofing=off","power=on","stop_time=1","temp_set=24","wind_speed=low"]
但我希望在tags
中获得更多细节值,例如:air是?权力是? temp_set是?
如何在tags
中获取更详细的数据?
答案 0 :(得分:2)
您可以将tags
的文本结果传递给JSON class
的构造函数并对其进行迭代,同时将每个字符串的所有"="
替换为" is "
<强>样品:强>
for(int i=0 ; i < jsonData.length() ; i++)
{
JSONObject jsonOBject = jsonData.getJSONObject(i);
String tags = jsonOBject.getString("tags");
JSONArray arr = new JSONArray(tags);
for(int i = 0 ; i < arr.length(); i++)
Log.d("VALUE", arr.getString(i).replaceAll("=", " is "));
}
<强>结果:强>
08-19 01:43:32.169: D/VALUE(1444): air is off
08-19 01:43:32.169: D/VALUE(1444): eco is off
08-19 01:43:32.175: D/VALUE(1444): freeze_proofing is off
08-19 01:43:32.175: D/VALUE(1444): power is on
08-19 01:43:32.175: D/VALUE(1444): stop_time is 1
08-19 01:43:32.189: D/VALUE(1444): temp_set is 24
08-19 01:43:32.195: D/VALUE(1444): wind_speed is low
答案 1 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
try{
String jsonResponse = "{\"id\":10541167124,\"title\":\"air-conditioner\",\"private\":\"false\",\"feed\":\"https://api.xively.com/v2/feeds/10541167124.json\",\"status\":\"frozen\",\"updated\":\"2014-08-15T08:23:01.612382Z\",\"created\":\"2014-08-15T08:17:17.336198Z\",\"creator\":\"https://xively.com/users/x22819\",\"version\":\"1.0.0\",\"datastreams\":[{\"id\":\"air-aonditioner1\",\"current_value\":\"25\",\"at\":\"2014-08-15T08:23:01.521837Z\",\"max_value\":\"25.0\",\"min_value\":\"25.0\",\"tags\":[\"air=off\",\"eco=off\",\"freeze_proofing=off\",\"power=on\",\"stop_time=1\",\"temp_set=24\",\"wind_speed=low\"],\"unit\":{\"symbol\":\"C\",\"label\":\"Celsius\"}}],\"product_id\":\"uq8_wTGk4Kdmfmlaa1B-\",\"device_serial\":\"7AHJ84EAQDEZ\"}";
JSONObject dataJson = new JSONObject(jsonResponse);
HashMap<String,Object> row = new HashMap<String, Object>();
row.put("id", dataJson.getString("id"));
row.put("title",dataJson.getString("title"));
row.put("private",dataJson.getString("private"));
row.put("feed",dataJson.getString("feed"));
row.put("status",dataJson.getString("status"));
row.put("updated",dataJson.getString("updated"));
row.put("created",dataJson.getString("created"));
row.put("creator",dataJson.getString("creator"));
row.put("version",dataJson.getString("version"));
ArrayList<HashMap<String, Object>> datastreamsList = new ArrayList<HashMap<String, Object>>();
JSONArray datastreamsjsonArray = dataJson.getJSONArray("datastreams");
for (int i=0;i<datastreamsjsonArray.length();i++){
HashMap<String,Object> datastreamsMap = new HashMap<String, Object>();
datastreamsMap.put("id",datastreamsjsonArray.getJSONObject(i).getString("id"));
datastreamsMap.put("current_value",datastreamsjsonArray.getJSONObject(i).getString("current_value"));
datastreamsMap.put("at",datastreamsjsonArray.getJSONObject(i).getString("at"));
datastreamsMap.put("max_value",datastreamsjsonArray.getJSONObject(i).getString("max_value"));
datastreamsMap.put("min_value",datastreamsjsonArray.getJSONObject(i).getString("min_value"));
JSONArray tagsJsonArray = datastreamsjsonArray.getJSONObject(i).getJSONArray("tags");
ArrayList<String> tagsList = new ArrayList<String>();
for (int j=0;j<tagsJsonArray.length();j++){
tagsList.add(tagsJsonArray.getString(j));
}
datastreamsMap.put("tags",tagsList);
HashMap<String,String> unitMap = new HashMap<String, String>();
unitMap.put("symbol",datastreamsjsonArray.getJSONObject(i).getJSONObject("unit").getString("symbol"));
unitMap.put("label",datastreamsjsonArray.getJSONObject(i).getJSONObject("unit").getString("label"));
datastreamsMap.put("unit",unitMap);
datastreamsList.add(datastreamsMap);
}
row.put("datastreams",datastreamsList);
row.put("product_id",dataJson.getString("product_id"));
row.put("device_serial",dataJson.getString("device_serial"));
System.out.print("id :"+row.get("id"));
System.out.print("title :"+row.get("title"));
System.out.print("private :"+row.get("private"));
System.out.print("feed :"+row.get("feed"));
System.out.print("status :"+row.get("status"));
System.out.print("updated :"+row.get("updated"));
System.out.print("created :"+row.get("created"));
System.out.print("creator :"+row.get("creator"));
System.out.print("version :"+row.get("version"));
ArrayList<HashMap<String,Object>> datastreamsListData = (ArrayList<HashMap<String,Object>>)row.get("datastreams");
for (int i=0;i<datastreamsListData.size();i++){
HashMap<String,Object> datastreamsMapData = datastreamsListData.get(i);
System.out.print("id :" + datastreamsMapData.get("id"));
System.out.print("current_value :" + datastreamsMapData.get("current_value"));
System.out.print("at :"+datastreamsMapData.get("at"));
System.out.print("max_value :" + datastreamsMapData.get("max_value"));
System.out.print("min_value :"+datastreamsMapData.get("min_value"));
ArrayList<String> tagsListData = (ArrayList<String>) datastreamsMapData.get("tags");
for (int j=0;j<tagsListData.size();j++){
System.out.print(tagsListData.get(j));
}
HashMap<String,Object> unitMapData = (HashMap<String,Object>) datastreamsMapData.get("unit");
System.out.print("symbol :"+unitMapData.get("symbol"));
System.out.print("label :"+unitMapData.get("label"));
}
System.out.print("product_id :"+row.get("product_id"));
System.out.print("device_serial :"+row.get("device_serial"));
}catch (Throwable e){
e.printStackTrace();
}
答案 2 :(得分:0)
问题是标签位于JSONArray中,因此请改用以下代码:
try {
JSONObject jsonRoot = new JSONObject(resultJSON);
JSONArray jsonData = jsonRoot.getJSONArray("datastreams");
for (int i = 0 ; i < jsonData.length() ; i++) {
JSONObject jsonOBject = jsonData.getJSONObject(i);
JSONArray arrTags = jsonOBject.getJSONArray("tags");
for (int j = 0; j < arrTags.length(); j++) {
Log.i("Array Tag [ "+ i +" ] ", arrTags.opt(j).toString());
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}