Reputation: 7288
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!
Upvotes: 5
Views: 3145
Reputation: 12674
Before parse any JSON string. create your JSON String like this
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): ]
Upvotes: 1
Reputation: 20155
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")));
}
Upvotes: 6
Reputation: 24857
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");
Upvotes: 5
Reputation: 6095
Format your json like this:
{
"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 verify correctness.
Upvotes: 3
Reputation: 360782
Your JSON has syntax errors. A number of the lines are missing commas, e.g.
"previewurl": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png"
"url": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png"
Upvotes: 4