【发布时间】:2014-10-13 13:34:53
【问题描述】:
我有一个 JSON 字符串,我正在尝试从中检索信息。 Json 字符串看起来像这样。
JSON 字符串:
{
"information": {
"device": {
"id": 0
},
"user": {
"id": 0
},
"data": [
{
"datum": {
"id": "00GF001",
"history_id": "9992BH",
"name": "abc",
"marks": 57,
"class": "B",
"type": "Student"
}
},
{
"datum": {
"id": "72BA9585",
"history_id": "78NAH2",
"name": "ndnmanet",
"marks": 70,
"class": "B",
"type": "Student"
}
},
{
"datum": {
"id": "69AHH85",
"history_id": "NN00E3006",
"name": "kit",
"department": "EF003",
"class": "A",
"type": "Employee"
}
},
{
"datum": {
"id": "09HL543",
"history_id": "34QWFTA",
"name": "jeff",
"department": "BH004",
"class": "A1",
"type": "Employee_HR"
}
}
]
}
}
我正在尝试从中访问数据 JSONArray 和相应的 Datum。我根据学生、员工等类型区分每个数据,并在哈希图中推送信息。 我在 javascript 中成功地做到了,但在 Java 中我很挣扎。
当我尝试访问 JSONArray 时,它会抛出异常
try {
JSONObject data = new JSONObject(dataInfo);
// Log.d(TAG, "CHECK"+data.toString());
JSONObject info = data.optJSONObject("information");
if(info.getJSONArray("data").getString(0).equals("Student") > 0) //exception here
Log.d(TAG, "Data"+ data.getJSONArray("data").length()); //exception here too
for(int m = 0; m < data.length(); m++){
// for(int s = 0; s < data[m].ge)
}
} catch (JSONException j){
j.printStackTrace();
}
我有任何用于创建相应类型的 hashmap 的指针。赞赏
【问题讨论】:
-
您提供的代码没有足够的信息来完全帮助您。例如,
current是什么?除非我遗漏了什么,否则您要返回的 JSON 中没有"signals"? -
对不起,我已经编辑了,但是如何从这个 json 字符串访问类型