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

时间:2015-01-27 21:35:26

标签: java android json parsing

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

你知道它为什么不起作用吗?

2 个答案:

答案 0 :(得分:0)

这是方式:

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");

答案 1 :(得分:0)

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

您可以查看此网站的JSON回复。

1)http://jsonlint.com/

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

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