我想读取json属性值,我的json格式就像这样
{"content":{"@attributes" : { "start_limit" : "x","end_limit" : "x","total_records" : "x"},"item":[{"category":"x","distance":"x"}]}},
我想从json属性中读取total_record的值。我怎么读?请帮帮我
由于 约翰里克
答案 0 :(得分:4)
首先检查你的JSON String.Change it to
来自的 {"content":{"@attributes" : { "start_limit" :"x","end_limit" : "x","total_records" : "x"}},"item":[{"category":"x","distance':"x"}]}}
{"content":{"@attributes" : { "start_limit" : "x","end_limit" : "x","total_records" : "x"},"item":[{"category":"x","distance":"x"}]}}
你在“item”之前留下一个结束大括号。
现在,下面是获取“total_records”值的代码
String jsonString = "{'content':{'@attributes' : { 'start_limit' :'x','end_limit' : 'x','total_records' : 'x'}},'item':[{'category':'x','distance':'x'}]}}";
public String getTotalRecords(String jsonString){
try {
JSONObject mainObject = new JSONObject(jsonString);
JSONObject contentObject = mainObject.getJSONObject("content");
JSONObject attributesObject = contentObject.getJSONObject("@attributes");
String totalRecords = attributesObject.getString("total_records");
Log.i("Total Records", totalRecords);
} catch (JSONException e) {
e.printStackTrace();
}
return totalRecords;
}
希望你能理解这个问题。
答案 1 :(得分:2)
只需使用JSONObject
。
JSONObject obj = new JSONObject(src);
String totalRecs = obj.getString("total_records");
不是100%确定它有效,但它是一个很好的例子从哪里开始。
答案 2 :(得分:0)
希望你在问过之前已经试过了。如果没有,这里有几个网址(我用Google搜索): http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/和http://about-android.blogspot.com/2010/03/androind-json-parser.html