2 qq 27665781 qq_27665781 于 2016.03.18 16:01 提问

Android 这Json应该怎么解析呢?【大家别嫌少,只有这点了】 1C

是用的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
}
]
]

9个回答

qq_33219517
qq_33219517   2016.03.18 16:07

你这是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,参考下
qq_27665781
qq_27665781 需求是要用 RestTemplate 解析
接近 2 年之前 回复
qq_17250009
qq_17250009   2016.03.18 16:06

去掉最外层[]之后是个JSONArray,之后正常解析

qq_27665781
qq_27665781 需求是要用 RestTemplate 解析
接近 2 年之前 回复
u014386018
u014386018   2016.03.18 16:08

是个jsonarray

qq_27665781
qq_27665781 需求是要用 RestTemplate 解析
接近 2 年之前 回复
cyxevil
cyxevil   2016.03.18 16:10

你先用bejson把你json的数据格式弄好
然后可以用android的角色onarray和jsonobject解析
不会的会的话,用gson也可以

cyxevil
cyxevil 回复qq_27665781: 我一般都是json解析json ,你说的那个我不了解
一年多之前 回复
qq_27665781
qq_27665781 需求是要用 RestTemplate 解析
接近 2 年之前 回复
luochoudan
luochoudan   Ds   Rxr 2016.03.18 16:15

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

qq_27665781
qq_27665781 需求是要用 RestTemplate 解析
接近 2 年之前 回复
qq_27665781
qq_27665781   Rxr 2016.03.18 16:30

图片说明

可是我用的是这种解析方法 ,

u013772876
u013772876   2016.03.18 16:44

是个jsonarray用list类型解析!

qq_27665781
qq_27665781 需求是要用 RestTemplate 解析
接近 2 年之前 回复
dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.03.18 18:02

为什么要用RestTemplate》Gson不是很好吗?

lxk_1993
lxk_1993   Rxr 2016.03.18 19:01

百度 一把大堆 这种问题 自己百度可以解决的

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!