问题描述
- Android 这Json应该怎么解析呢?【大家别嫌少,只有这点了】
-
是用的RestTemplate解析[
[
{
"id": 435224,
"calcTime": "2016-03-16 20:20:52.0",
"unit": null,
"deviceCode": {
"id": 32,
"devicename": "主机1"
},
"value": 0
},
{
"id": 435220,
"calcTime": "2016-03-16 20:15:52.0",
"unit": null,
"deviceCode": {
"id": 32,
"devicename": "主机1"
},
"value": 0
},
{
"id": 435216,
"calcTime": "2016-03-16 20:10:52.0",
"unit": null,
"deviceCode": {
"id": 32,
"devicename": "主机1"
},
"value": 0
},
{
"id": 435212,
"calcTime": "2016-03-16 20:05:52.0",
"unit": null,
"deviceCode": {
"id": 32,
"devicename": "主机1"
},
"value": 0
},
{
"id": 435208,
"calcTime": "2016-03-16 20:00:52.0",
"unit": null,
"deviceCode": {
"id": 32,
"devicename": "主机1"
},
"value": 0
}
],
[
{
"id": 435225,
"calcTime": "2016-03-16 20:20:52.0",
"unit": null,
"deviceCode": {
"id": 33,
"devicename": "主机2"
},
"value": 0
},
{
"id": 435221,
"calcTime": "2016-03-16 20:15:52.0",
"unit": null,
"deviceCode": {
"id": 33,
"devicename": "主机2"
},
"value": 0
},
{
"id": 435217,
"calcTime": "2016-03-16 20:10:52.0",
"unit": null,
"deviceCode": {
"id": 33,
"devicename": "主机2"
},
"value": 0
},
{
"id": 435213,
"calcTime": "2016-03-16 20:05:52.0",
"unit": null,
"deviceCode": {
"id": 33,
"devicename": "主机2"
},
"value": 0
},
{
"id": 435209,
"calcTime": "2016-03-16 20:00:52.0",
"unit": null,
"deviceCode": {
"id": 33,
"devicename": "主机2"
},
"value": 0
}
],
[
{
"id": 435226,
"calcTime": "2016-03-16 20:20:52.0",
"unit": null,
"deviceCode": {
"id": 34,
"devicename": "主机3"
},
"value": 0
},
{
"id": 435222,
"calcTime": "2016-03-16 20:15:52.0",
"unit": null,
"deviceCode": {
"id": 34,
"devicename": "主机3"
},
"value": 0
},
{
"id": 435218,
"calcTime": "2016-03-16 20:10:52.0",
"unit": null,
"deviceCode": {
"id": 34,
"devicename": "主机3"
},
"value": 0
},
{
"id": 435214,
"calcTime": "2016-03-16 20:05:52.0",
"unit": null,
"deviceCode": {
"id": 34,
"devicename": "主机3"
},
"value": 0
},
{
"id": 435210,
"calcTime": "2016-03-16 20:00:52.0",
"unit": null,
"deviceCode": {
"id": 34,
"devicename": "主机3"
},
"value": 0
}
],
[
{
"id": 435227,
"calcTime": "2016-03-16 20:20:52.0",
"unit": null,
"deviceCode": {
"id": 35,
"devicename": "主机4"
},
"value": 0
},
{
"id": 435223,
"calcTime": "2016-03-16 20:15:52.0",
"unit": null,
"deviceCode": {
"id": 35,
"devicename": "主机4"
},
"value": 0
},
{
"id": 435219,
"calcTime": "2016-03-16 20:10:52.0",
"unit": null,
"deviceCode": {
"id": 35,
"devicename": "主机4"
},
"value": 0
},
{
"id": 435215,
"calcTime": "2016-03-16 20:05:52.0",
"unit": null,
"deviceCode": {
"id": 35,
"devicename": "主机4"
},
"value": 0
},
{
"id": 435211,
"calcTime": "2016-03-16 20:00:52.0",
"unit": null,
"deviceCode": {
"id": 35,
"devicename": "主机4"
},
"value": 0
}
]
]
解决方案
你这是json数组呀,参考下这个。
import org.json.JSONArray;
import org.json.JSONObject;
//jsonData的数据格式:[{ "id": "27JpL~jd99w9nM01c000qc", "version": "abc" },{ "id": "27JpL~j6UGE0LX00s001AH", "version": "bbc" },{ "id": "27JpL~j7YkM0LX01c000gt", "version": "Wa_" }]
JSONArray arr = new JSONArray(jsonData);
for (int i = 0; i < arr.length(); i++) {
JSONObject temp = (JSONObject) arr.get(i);
String id = temp.getString("id");
String id = temp.getString("version");
}
有一篇博客,http://blog.csdn.net/dwarven/article/details/7561563,参考下
解决方案二:
去掉最外层[]之后是个JSONArray,之后正常解析
解决方案三:
是个jsonarray
解决方案四:
你先用bejson把你json的数据格式弄好
然后可以用android的角色onarray和jsonobject解析
不会的会的话,用gson也可以
解决方案五:
参考
try {
JSONArray parentArray = new JSONArray("");
JSONArray sonArray = parentArray.getJSONArray(0);
for (int i = 0; i < sonArray.length(); i++) {
JSONObject jsonObject = sonArray.getJSONObject(i);
System.out.println("jsonObject: " + jsonObject);
Long id = jsonObject.getLong("id");
String calcTime = jsonObject.getString("calcTime");
String unit = jsonObject.getString("unit");
JSONObject deviceCode = jsonObject.getJSONObject("jsonObject");
Long deviceCodeId = deviceCode.getLong("id");
String devicename = deviceCode.getString("deviceCode");
Integer value = jsonObject.getInt("value");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
可是我用的是这种解析方法 ,
解决方案七:
是个jsonarray用list类型解析!
解决方案八:
为什么要用RestTemplate》Gson不是很好吗?
解决方案九:
百度 一把大堆 这种问题 自己百度可以解决的