如何使用android读取json中的属性值

发布于 2024-10-28 00:38:30 字数 255 浏览 0 评论 0原文

我想读取json属性值,我的json格式像这样

{"content":{"@attributes" : { "start_limit" : "x","end_limit" : "x","total_records" : "x"},"item":[{"category":"x","distance":"x"}]}},

我想从json属性读取total_record的值。我怎样才能阅读?请帮助我

谢谢 约翰·瑞克

I want to read json attribute value, my json format like this

{"content":{"@attributes" : { "start_limit" : "x","end_limit" : "x","total_records" : "x"},"item":[{"category":"x","distance":"x"}]}},

I want to read total_record's value from json attributes. How can I read ? Please help me

Thanks
John Rick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁的年少不轻狂 2024-11-04 00:38:30

首先检查您的 JSON 字符串。将其更改为

{"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;
}

希望您理解这个问题。

First of all check your JSON String.Change it to

{"content":{"@attributes" : { "start_limit" :"x","end_limit" : "x","total_records" : "x"}},"item":[{"category":"x","distance':"x"}]}} from

{"content":{"@attributes" : { "start_limit" : "x","end_limit" : "x","total_records" : "x"},"item":[{"category":"x","distance":"x"}]}}

In which you left one closing curly brace before "item".

Now below is the code to get value of "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;
}

Hope you understand the problem.

财迷小姐 2024-11-04 00:38:30

只需使用 JSONObject 即可。

JSONObject obj = new JSONObject(src);
String totalRecs = obj.getString("total_records");

不能 100% 确定它有效,但它是一个很好的起点。

Simply use JSONObject.

JSONObject obj = new JSONObject(src);
String totalRecs = obj.getString("total_records");

Not 100% sure it works, but it is a good example where to start.

吻安 2024-11-04 00:38:30

希望您在提出问题之前已经尝试过。如果没有,这里有一些网址(我用谷歌搜索):
http://www.androidcompetencycenter.com/2009/10/json- parse-in-android/http:// about-android.blogspot.com/2010/03/androind-json-parser.html

Hope you've already given it a try before asking in SO. If not, here are a few urls (which I googled):
http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/ and http://about-android.blogspot.com/2010/03/androind-json-parser.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文