Create parsed JSON file for android application

I am trying to get a list of images in a json file that I have on my web server with my Android app. But they are not readable, I must have made a mistake, probably in my json file.

I am trying to create a .Json file that my application can read, one of my experimental JSON files is listed below, but it does not work.

Since I am not very versed in Json, I was wondering if anyone else could find out how to create a JSON file that my application can handle.

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 is:

 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!

+5
source share
5 answers

Then your json should look 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"
    }
]

JSONString JSONObject JSONArray json, .

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")));
}
+6

, JSONArray, JSONObject.

:

JSONArray array = new JSONArray(data);

:

JSONObject rootObject = new JSONObject(data);
JSONArray array = rootObject.optJSONArray("wallpaper");
+5

Your JSON has syntax errors. A number of lines do not contain commas, for example

    "previewurl": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png"
    "url": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png"
+4
source

Format your json as follows:

{
    "wallpaper": [
        {
            "id": "1",
            "title": "Clouds",
            "thumburl": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png", 
            "previewurl": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png", <--- You were missing a comma here
            "url": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png", <-- and here
            "text": "Sky"
        }
    ]
}

In the future, you can use JSON Lint to check for correctness.

+3
source

Before parsing any JSON string. create your JSON String as follows

try {

   JSONObject wallpaper=new JSONObject();
   wallpaper.put("id", "1");
   wallpaper.put("title", "Clouds");
   wallpaper.put("thumburl", "http://sherdle.com/images/Pages/Apps/Sherdleapps.png");
   wallpaper.put("previewurl", "http://sherdle.com/images/Pages/Apps/Sherdleapps.png");
   wallpaper.put("url", "http://sherdle.com/images/Pages/Apps/Sherdleapps.png");
   wallpaper.put("text", "Sky");
   JSONArray wallpaer_array=new JSONArray();
   wallpaer_array.put(wallpaper);
   Log.d("json :",wallpaer_array.toString(0));

  } catch (JSONException e) {
   e.printStackTrace();
  }

Logcat:

05-06 11:05:51.253: D/json :(434): [
05-06 11:05:51.253: D/json :(434): {
05-06 11:05:51.253: D/json :(434): "id": "1",
05-06 11:05:51.253: D/json :(434): "thumburl": "http:\/\/sherdle.com\/images\/Pages\/Apps\/Sherdleapps.png",
05-06 11:05:51.253: D/json :(434): "text": "Sky",
05-06 11:05:51.253: D/json :(434): "title": "Clouds",
05-06 11:05:51.253: D/json :(434): "previewurl": "http:\/\/sherdle.com\/images\/Pages\/Apps\/Sherdleapps.png",
05-06 11:05:51.253: D/json :(434): "url": "http:\/\/sherdle.com\/images\/Pages\/Apps\/Sherdleapps.png"
05-06 11:05:51.253: D/json :(434): }
05-06 11:05:51.253: D/json :(434): ]
+1
source

All Articles