阅读与改造JSON [英] Reading JSON with Retrofit

查看:179
本文介绍了阅读与改造JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON提要:

I've got the following JSON feed:

{
  collection_name: "My First Collection",
  username: "Alias",
  collection: {
     1: {
        photo_id: 1,
        owner: "Some Owner",
        title: "Lightening McQueen",
        url: "http://hesp.suroot.com/elliot/muzei/public/images/randomhash1.jpg"
        },
     2: {
        photo_id: 2,
        owner: "Awesome Painter",
        title: "Orange Plane",
        url: "http://hesp.suroot.com/elliot/muzei/public/images/randomhash2.jpg"
        }
    }
}

我所试图做的就是收集的内容 - photo_id,老板,标题和URL。我有以下的code,但我越来越GSON JSON错误:

What I am trying to do is get the contents of the collection - photo_id, owner, title and URL. I have the following code, however I am getting GSON JSON errors:

   @GET("/elliot/muzei/public/collection/{collection}")
    PhotosResponse getPhotos(@Path("collection") String collectionID);

    static class PhotosResponse {
        List<Photo> collection;
    }

    static class Photo {
        int photo_id;
        String title;
        String owner;
        String url;
    }
}

我想我的code是正确的,以获得JSON的饲料,但我不那么肯定。任何帮助AP preciated。

I thought my code was correct to get the JSON feed, however I'm not so sure. Any help appreciated.

我得到的错误是:

Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 75

不过我努力理解如何使用GSON库

However I'm struggling to understand how to use the GSON library

推荐答案

您的JSON是无效的。

Your JSON is not valid.

GSON正在等待 BEGIN_ARRAY[系列:,因为你的 PhotosResponse 类中定义照片的数组列表&LT;摄影&GT; ,但发现了一个 BEGIN_OBJECT{ ,它应该是

GSON is waiting for a BEGIN_ARRAY "[" after collection: because your PhotosResponse class define an array of Photo List<Photo> but found a BEGIN_OBJECT "{", it should be

{
    "collection_name": "My First Collection",
    "username": "Alias",
    "collection": [
        {
            "photo_id": 1,
            "owner": "Some Owner",
            "title": "Lightening McQueen",
            "url": "http://hesp.suroot.com/elliot/muzei/public/images/randomhash1.jpg"
        },
        {
            "photo_id": 2,
            "owner": "Awesome Painter",
            "title": "Orange Plane",
            "url": "http://hesp.suroot.com/elliot/muzei/public/images/randomhash2.jpg"
        }
    ]
}

也许你得到JSON从一个不正确的 json_en code() PHP数组与关键,你应该带code JSON从PHP带钥匙,用使用json_en只( PHP数组到JSON数组数组值code()

maybe you get that JSON from an incorrect json_encode() PHP array with key, you should encode JSON from PHP without keys, with the array values only (PHP Array to JSON Array using json_encode())

这篇关于阅读与改造JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆