Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a parsable JSON file for android application

Tags:

json

android

I'm trying to get a list of images in a json file I have on my webserver with my android application. But they are not being read, I must have made some mistake, probably in my json file.

I'm trying to create a .Json file my application can read, one of my experimental JSON files is listed below, but it's not working.

Since I'm not very experienced with Json I was wondering if somebody else might know how to create a JSON file my application can parse.

My experimental json file:

{
"Wallpaper": [
    {
        "id": "1",
        "title": "Clouds",
        "thumburl": "http://url.com/images/Pages/Apps/apps.png",
        "previewurl": "http://url.com/images/Pages/Apps/apps.png",
        "url": "http://url.com/images/Pages/Apps/apps.png",
        "text": "Sky"
    }
]
}

And my code:

 import someimportsandotherstuff

 import de.dan_nrw.android.scroid.Wallpaper;


 public final class JsonWallpaperParser implements IWallpaperParser {

/**
 * Creates a new instance of JsonWallpaperParser.
 */
JsonWallpaperParser() {
    super();
}


/* (non-Javadoc)
 * @see de.dan_nrw.boobleftboobright.IWallpaperParser#parse(java.lang.String)
 */
@Override
public List<Wallpaper> parse(String data) throws ParseException {
    try {
        JSONArray array = new JSONArray(data);
        List<Wallpaper> wallpapers = new ArrayList<Wallpaper>();

        for (int i = 0; i < array.length(); i++) {
            JSONObject jsonWallpaper = array.getJSONObject(i);

            wallpapers.add(new Wallpaper(jsonWallpaper.getString("id"),
                                         jsonWallpaper.getString("title"),
                                         URI.create(jsonWallpaper.getString("thumburl")),
                                         URI.create(jsonWallpaper.getString("previewurl")),
                                         URI.create(jsonWallpaper.getString("url")),
                                         jsonWallpaper.getString("text")));
        }

        return wallpapers;
    }
    catch (JSONException ex) {
        throw new ParseException(ex.getMessage(), 0);
    }           
}
 }

Any help is appreciated!

like image 766
Mdlc Avatar asked Apr 26 '13 14:04

Mdlc


2 Answers

Then your json should be like this

[
    {
        "id": "1",
        "title": "Clouds",
        "thumburl": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png",
        "previewurl": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png",
        "url": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png",
        "text": "Sky"
    }
]

Your JSONString returning JSONObject not JSONArray You should parse your json string like this

JSONObject object=new JSONObject(data);
JSONArray array=object.getJSONArray("wallpaper");
List<Wallpaper> wallpapers = new ArrayList<Wallpaper>();
for (int i = 0; i < array.length(); i++) {
    JSONObject jsonWallpaper = array.getJSONObject(i);

    wallpapers.add(new Wallpaper(jsonWallpaper.getString("id"),
                                 jsonWallpaper.getString("title"),
                                 URI.create(jsonWallpaper.getString("thumburl")),
                                 URI.create(jsonWallpaper.getString("previewurl")),
                                 URI.create(jsonWallpaper.getString("url")),
                                 jsonWallpaper.getString("text")));
}
like image 139
Pragnani Avatar answered Sep 27 '22 17:09

Pragnani


Your problem is you're trying to create a JSONArray when your root element is a JSONObject.

This line is incorrect:

JSONArray array = new JSONArray(data);

You should change this to:

JSONObject rootObject = new JSONObject(data);
JSONArray array = rootObject.optJSONArray("wallpaper");
like image 35
Bobbake4 Avatar answered Sep 27 '22 17:09

Bobbake4