【问题标题】:JSON parsing with JSONObject not working使用 JSONObject 解析 JSON 不起作用
【发布时间】:2015-01-27 21:35:25
【问题描述】:

我有一个这样的 json 字符串:

{
"ip":"10.41.X.X",
"board":
    {
        "projets":{},
        "notes":{},
        "susies":{},
        "activites":{},
        "modules":{},
        "stages":{},
        "tickets":{}
    }
"history":
    {
        {
            "title":"You have joined the activity Corrections Evaluation - M\u00e9mo professionnel<\/a>"
            "user":
                { 
                    "picture":"https:\/\/cdn.local.epitech.eu\/userprofil\/amsell_j.bmp", 
                    "title":"Jeremie Amsellem", 
                    "url":"\/user\/amsell_j\/" 
                }, 
            "content":"Remember to validate your presence with your token View other registered people ...<\/a>", "date":"2014-11-23 18:24:42", 
            "id":"6557808", 
            "visible":"1", 
            "id_activite":"173479", 
            "class":"register"
        }
    }
    "infos":
        { 
            "id":"42891", 
            "login":"amsell_j", 
            "title":"Jeremie Amsellem", 
            "email":null, "internal_email":"amsell_j@epitech.eu", 
            "lastname":"Amsellem", 
            "firstname":"Jeremie", 
            "userinfo":{}
            "referent_used":true, 
            "picture":"amsell_j.bmp", 
            "picture_fun":null, 
            "email_referent":"email@email.com", 
            "pass_referent":"0000", 
            "promo":2017, 
            "semester":5, 
            "uid":110268, 
            "gid":32017, 
            "location":"FR\/PAR", 
            "documents":"vrac\/amsell_j", 
            "userdocs":"\/u\/epitech_2017\/amsell_j\/cu", 
            "shell":"\/usr\/site\/bin\/shell", 
            "netsoul":null, 
            "close":false, 
            "close_reason":null, 
            "ctime":"2013-12-06 04:00:56", 
            "mtime":"2013-11-22 18:00:05", 
            "comment":null, 
            "id_promo":"279", 
            "id_history":"144269", 
            "course_code":"bachelor\/classic", 
            "school_code":"epitech", 
            "school_title":"epitech", 
            "old_id_promo":"244,250,255,254,272", 
            "old_id_location":"4", 
            "rights":{ }, 
            "invited":true, 
            "studentyear":3, 
            "admin":false,
    }
    "current":
    { 
        "active_log":"0.9069", 
        "credits_min":"120", 
        "credits_norm":"120", 
        "credits_obj":"150", 
        "nslog_min":"15", 
        "nslog_norm":"25", 
        "semester_code":"B5", 
        "semester_num":"5", 
        "achieved":124, 
        "failed":63, 
        "inprogress":39 
    } 
}

我想恢复“历史”下“图片”中的字符串。 我做了以下代码,但它不起作用:

JSONObject jsonObject = new JSONObject(toParse);
JSONObject history = jsonObject.getJSONObject("history");
Log.d("test", history.getString("title"));

如果我这样做,例如它可以正常工作,则 id 会很好地显示:

 JSONObject jsonObject = new JSONObject(toParse);
 JSONObject history = jsonObject.getJSONObject("infos");
 Log.d("test", history.getString("id"));

你知道它为什么不工作吗?

【问题讨论】:

  • 您的 json 对象之间缺少逗号。
  • title 是嵌套在历史对象中的对象的属性,为什么要多出括号?
  • 收到的 json 是由我学校的 API 提供的,我必须在“历史”中恢复图片的路径。 API 在这里:epitech-api.herokuapp.com
  • 看起来像错字,“历史”:[{...}] 更有意义
  • @JeromeCampeaux - 使用您自己的凭据和一些 REST 客户端(Chrome 有一个名为 POSTMAN 的好插件) - 获取实际数据,而不是网站上格式错误的 JSON。然后你就可以真正看到数据是什么了。

标签: java android json parsing


【解决方案1】:

是这样的:

JSONObject jsonObject = new JSONObject(toParse);
JSONObject history = jsonObject.getJSONObject("history");
JSONObject user = history.getJSONObject("user");
String title = user.getString("title");
String picture = user.getString("picture");

【讨论】:

  • 问题出在你的 json 数据中
【解决方案2】:

在解析之前,首先检查你的 JSON 响应是否有效。

您可以查看此网站的 JSON 响应。

1) http://jsonlint.com/

2) http://jsonviewer.stack.hu/ 这是按特定顺序格式化 JSON 响应。

更正您的 JSON 响应后,尝试解析它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多