如何使用Gson解析这个Json?
问题描述:
我知道如何解析json,但我无法解决此json中的整数json字段。因为这个json包含整数字段(23254,23998)。如何使用Gson解析这个Json?
这里我JSON
{
"status":"OK",
"alarms":{
"23254":[
{
"speed_limit":250,
"acc_limit":null,
"dcc_limit":null,
"idle_limit":null
}
],
"23998":[
{
"speed_limit":120,
"acc_limit":null,
"dcc_limit":null,
"idle_limit":null
}
]
}
}
答
try{
JSONObject j=new JSONObject("here put your response string");
JSONObject j1= j.getJSONObject("alarms");
ArrayList<String> arr=new ArrayList<String>();
Iterator iter = j1.keys();
while(iter.hasNext()){
String key = (String)iter.next();
arr.add(j1.getJSONArray(key).toString());
System.out.println("json"+j1.getJSONArray(key).toString());
}
}catch (JSONException e){
e.printStackTrace(); }
答
解析JSON对象像这样
JSONObject obj= new JSONObject(content);
Iterator iterator = obj.keys();
while(iterator.hasNext()){
String key = (String)iterator.next();
JSONObject object1 = obj.getJSONObject(key);
// get id from object1
String _pubKey = object1.optString("id");
}
参考http://stackoverflow.com/questions/8371274/how-to-parse-json-array-in-android-with-gson – sasikumar
你可以尝试使用'INT上限= .getInt(“speed_limit “)' –
Mohit
可能重复的[如何在Java中解析JSON](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Selvin