简体   繁体   English

Android:Java:不使用GSON或jackson进行JSON解析

[英]Android: Java: JSON parsing without using GSON or jackson

I am new to JSON ,I have following JSON String which i am getting from a server want to use each object item and traverse this JSON. 我是JSON的新手,我从服务器获取以下JSON字符串,以使用每个对象项并遍历此JSON。

No need to use GSON or Jackson Library. 无需使用GSON或Jackson库。 Video Id and thumb is most prior for me 视频ID和大拇指最适合我

     {'videos': [ { "video":                                                        {"duration":"2:51","views":36824,"video_id":"41141","rating":"4.25","ratings":"51","title":"                            video1","url":"http:\/\/www.xyz.com\/41141","default_thumb":"http:\/\/img02.xyz.com\/_thumbs   \/0000041\/0041141\/0041141_015m.jpg","thumb":"http:\/\/img02.xyz.com\/_thumbs\/0000041\/004      1141\/0041141_015m.jpg","publish_date":"2014-03-27 05:38:01"}},
  {"video":{"duration":"2:51","views":36825,"video_id":"4141","rating":"4.25","ratings":"51","title":"video2","url":"http:\/\/www.xyz.com\/4141","default_thumb":"http:\/\/img03.xyz.com\/_thumbs\/0000041\/0041141\/0041141_015m.jpg","thumb":"http:\/\/img03.xyz.com\/_thumbs\/0000041\/0041141\/0041141_015m.jpg","publish_date":"2014-03-27 05:38:01"}},
{ "video":{"duration":"2:51","views":36225,"video_id":"41412","rating":"4.25","ratings":"51","title":"video3","url":"http:\/\/www.xyz.com\/41412","default_thumb":"http:\/\/img04.xyz.com\/_thumbs\/0000041\/0041141\/0041141_015m.jpg","thumb":"http:\/\/img04.xyz.com\/_thumbs\/0000041\/004 1141\/0041141_016m.jpg","publish_date":"2014-03-27 05:38:01"}}
}}],"count":279369}
JSONArray videojarray   =jobj.getJSONArray("videos");
JSONObject videoJObject=null;
for(int j=0;j<videojarray.length();j++){
  videoJObject=videojarray.getJSONObject(j);
  JSONObject  videoJObj=videoJObject.getJSONObject("video");
  String videoid=videoJObj.getString("video_id");
  String thumb=videoJObj.getString("thumb");
}

Your JSON is not valid, change single quote ' to double quote " at the video on start of JSON, and remove two closing bracket } before count. Then try to parse your valid JSON. 您的JSON无效,请在JSON开头的视频中将单引号'改为双引号" ,并在计数前删除两个右括号} ,然后尝试解析有效​​的JSON。

A corrected JSON is the bellow: 正确的JSON如下所示:

{
    "videos": [
        {
            "video": {
                "duration": "2:51",
                "views": 36824,
                "video_id": "41141",
                "rating": "4.25",
                "ratings": "51",
                "title": "video1",
                "url": "http://www.xyz.com/41141",
                "default_thumb": "http://img02.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "thumb": "http://img02.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "publish_date": "2014-03-27 05:38:01"
            }
        },
        {
            "video": {
                "duration": "2:51",
                "views": 36825,
                "video_id": "4141",
                "rating": "4.25",
                "ratings": "51",
                "title": "video2",
                "url": "http://www.xyz.com/4141",
                "default_thumb": "http://img03.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "thumb": "http://img03.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "publish_date": "2014-03-27 05:38:01"
            }
        },
        {
            "video": {
                "duration": "2:51",
                "views": 36225,
                "video_id": "41412",
                "rating": "4.25",
                "ratings": "51",
                "title": "video3",
                "url": "http://www.xyz.com/41412",
                "default_thumb": "http://img04.xyz.com/_thumbs/0000041/0041141/0041141_015m.jpg",
                "thumb": "http://img04.xyz.com/_thumbs/0000041/0041141/0041141_016m.jpg",
                "publish_date": "2014-03-27 05:38:01"
            }
        }
    ],
    "count": 279369
} 

Try this.. 尝试这个..

JSONObject jobj = new JSONObject(response);
JSONArray videosjarray = jobj.getJSONArray("videos");
for(int j=0;j < videosjarray.length();j++){
  JSONObject videosJObject = videosjarray.getJSONObject(j);
  JSONObject video = videosJObject.getJSONObject("video");
  String videoid = video.getString("video_id");
  String thumb = video.getString("thumb");
}

Your JSON is not valid check it from this like http://jsonlint.com/ 您的JSON无效,请从http://jsonlint.com/这样检查

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

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