为Android应用程序创建一个可解析的JSON文件

时间:2013-04-26 14:47:35

标签: android json

我正在尝试使用我的Android应用程序在我的网络服务器上获取json文件中的图像列表。但它们没有被阅读,我一定是犯了一些错误,可能是在我的json文件中。

我正在尝试创建一个我的应用程序可以读取的.Json文件,下面列出了我的一个实验性JSON文件,但它无效。

由于我对Json不是很熟悉,我想知道其他人是否可能知道如何创建我的应用程序可以解析的JSON文件。

我的实验性json文件:

{
"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"
    }
]
}

我的代码:

 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);
    }           
}
 }

感谢任何帮助!

5 个答案:

答案 0 :(得分:6)

然后你的json应该是这样的

[
    {
        "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")));
}

答案 1 :(得分:5)

您的问题是,当您的根元素是JSONObject时,您正在尝试创建JSONArray。

此行不正确:

JSONArray array = new JSONArray(data);

您应该将其更改为:

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

答案 2 :(得分:4)

您的JSON有语法错误。许多行都缺少逗号,例如

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

答案 3 :(得分:3)

像这样格式化你的json:

{
    "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"
        }
    ]
}

将来,您可以使用JSON Lint来验证正确性。

答案 4 :(得分:1)

在解析任何JSON字符串之前。像这样创建你的JSON字符串

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): ]