使用JSON从URL解析字符串数据

时间:2014-01-12 23:55:10

标签: java android json parsing http

用JSON特别是(TemPreture和HumiDity)的值来解析这些数据的最佳方法是什么,我尝试了几种方法但没有运气

任何想法?

这是其中的字符串>>

{ "id":1174383824,"title":"LED Controler","private":"false","feed":"https://api.xively.com/v2/feeds/1174383824.json","status":"frozen","updated":"2014-01-09T15:33:47.492487Z","created":"2013-10-14T12:17:16.306982Z","creator":"https://xively.com/users/ahmed_soothsayer","version":"1.0.0","datastreams": [ {"id":"ALED","current_value":"N","at":"2014-01-09T11:31:15.517313Z","max_value":"0.0","min_value":"0.0"}, {"id":"ALED2","current_value":"N","at":"2014-01-09T11:31:25.343550Z","max_value":"0.0","min_value":"0.0"}, {"id":"ALED3","current_value":"N","at":"2014-01-09T11:31:47.862557Z","max_value":"0.0","min_value":"0.0"}, {"id":"ALED4","current_value":"N","at":"2014-01-09T11:31:37.998191Z","max_value":"0.0","min_value":"0.0"}, {"id":"ALED5","current_value":"P","at":"2014-01-09T15:33:47.264986Z","max_value":"0.0","min_value":"0.0"}, {"id":"HumiDity","current_value":"39.0","at":"2014-01- 09T08:47:17.426770Z","max_value":"3332.2","min_value":"0.0","unit":{"symbol":"%","label":"%"}}, {"id":"HumiDity2","current_value":"61.3","at":"2014-01- 09T08:47:09.933117Z","max_value":"70.2","min_value":"0.0","unit":{"symbol":"%","label":"%"}}, {"id":"Soil_Moisture","current_value":"0","at":"2014-01- 09T08:47:15.428252Z","max_value":"100.0","min_value":"0.0","unit":{"symbol":"%","label":"%"}}, {"id":"Sun_Light","current_value":"17","at":"2014-01-09T08:47:16.459234Z","max_value":"99.9","min_value":"0.0"}, {"id":"TemPreture","current_value":"27","at":"2014-01-09T08:49:19.779376Z","max_value":"66.0","min_value":"- 23.5","unit":{"symbol":"°C","label":"°C"}},{"id":"TemPreture2","current_value":"66.9","at":"2014-01- 09T08:49:35.854478Z","max_value":"66.9","min_value":"-23.4","unit": {"symbol":"°C","label":"°C"}}],"product_id":"2wQm6WoR-tYxbbyWgBNQ","device_

1 个答案:

答案 0 :(得分:0)

您可以使用Google的Gson进行解析。您需要下载库并将其添加到项目中。 https://code.google.com/p/google-gson/downloads/list

然后,您可以直接解析数据,也可以创建一个Java类来匹配结构,并允许gson将字符串反序列化为Java类。

原始反序列化示例:

int one = gson.fromJson("1", int.class);

对象反序列化示例:

BagOfPrimitives obj = gson.fromJson(jsonString, BagOfPrimitives.class); 

有关完整示例,请参阅: https://sites.google.com/site/gson/gson-user-guide#TOC-Primitives-Examples