繁体   English   中英

使用JSONObject进行JSON解析不起作用

[英]JSON parsing with JSONObject not working

我有一个像这样的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"));

您是否知道为什么它不起作用?

这是这样的:

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响应是否有效。

您可以从该网站检查JSON响应。

1) http://jsonlint.com/

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

更正JSON响应后,尝试对其进行解析。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM