5

我正在尝试使用我的 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);
    }           
}
 }

任何帮助表示赞赏!

4

5 回答 5

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")));
}
于 2013-04-26T15:20:08.513 回答
5

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

此行不正确:

JSONArray array = new JSONArray(data);

您应该将其更改为:

JSONObject rootObject = new JSONObject(data);
JSONArray array = rootObject.optJSONArray("wallpaper");
于 2013-04-26T15:20:38.850 回答
4

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

    "previewurl": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png"
    "url": "http://sherdle.com/images/Pages/Apps/Sherdleapps.png"
于 2013-04-26T14:50:23.410 回答
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来验证正确性。

于 2013-04-26T14:50:48.483 回答
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();
  }

日志猫:

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): ]
于 2013-05-06T05:41:02.040 回答