开发者

JSON Problem in android (objects disappear)

开发者 https://www.devze.com 2023-03-21 23:41 出处:网络
I\'m trying to make a small app (an image gallery from images from the web, were the url\'s I get from the JSON file that I received). The context of JSON looks like that:

I'm trying to make a small app (an image gallery from images from the web, were the url's I get from the JSON file that I received). The context of JSON looks like that:

{"images":{
    "yXVak":{
        "image_hash":"yXVak",
            "imgur_page":"http:\/\/imgur.com\/yXVak",
            "original_image":"http:\/\/imgur.com\/yXVak.gif",
            "large_thumbnail":"http:\/\/imgur.com\/yXVakl.gif",
            "small_thumbnail":"http:\/\/imgur.com\/yXVaks.gif",
                "message":"I didn't know they made you see THAT well.",
                "source":" ",
                    "date_popular":"2011-07-18 18:45:05"},

..... I have about 30 more objects that looks like "yXVak".

Now, the problem is, when I'm trying to parse the text, the program can't find the object "yXVak", the exception looks like that: org.json.JSONException: JSONObject["yXVak"] not found. I parse the JSON file like that:

jObject =  new JSONObject(jString);
                    JSONObject jImages =  jObject.getJSONObject("images");
                    getImages(jImages);

getImages function looks like that:

JSONObject  jHash = jImages.getJSONObject("yXVak") ;
           String hash = jHash.getString("yXVak");
           String page = jHash.getString("http:\\/\\/imgur.com\\/yXVak");
           Image[] images = new Image[3];
           images[0] = new Image(jHash.get开发者_如何学PythonString("original_image"), jHash.getString("http:\\/\\/imgur.com\\/yXVak.gif"));
           images[1] = new Image(jHash.getString("large_thumbnail"), jHash.getString("http:\\/\\/imgur.com\\/yXVakl.gif"));
           images[2] = new Image(jHash.getString("small_thumbnail"), jHash.getString("http:\\/\\/imgur.com\\/yXVaks.gif"));
           String message = jHash.getString("I didn't know they made you see THAT well.");
           String source = jHash.getString(" ");
           String date = jHash.getString("2011-07-18 18:45:05");
           listOfImages.add(new ImageHash(hash, page, images, message, source, date));

...

By debugging I found that the jString object looks right (the whole string that in the file), but the jImages object missing two first objects ("yXVak", and the second one that I didn't show here "6k9yE"). Can someone help me with that please, what did I do wrong?


I thing you should change this lines in your getImages function:-

JSONObject jHash = jImages.getJSONObject("yXVak");
//Changes in this lines.
String hash = jHash.getString("image_hash");
String page = jHash.getString("imgur_page");
// Rest of your code is same.

Please try this out.I think that it will solve your problem,

0

精彩评论

暂无评论...
验证码 换一张
取 消