JSON解析LoganSquare

时间:2015-06-16 20:24:24

标签: android json

我有一个json响应,我想用LoganSquare库解析它。

这是json数组的一个例子:

{
  "version": 1,
  "looks":
  [
    {
        "uuid": 10001,
        "img_url": "myUrl",
        "stylist": "myName",
        "address": "myAddress",
        "phone": "myPhone",
        "timetable": "myTimeTable",
        "latitude": "myLatitude",
        "longitude": "myLongitude"
    },
    {
        "uuid": 10002,
        "img_url": "myUrl2",
        "stylist": "myName2",
        "address": "myAddress2",
        "phone": "myPhone2",
        "timetable": "myTimeTable2",
        "latitude": "myLatitude2",
        "longitude": "myLongitude2"
    }
  ]
}

这是我用来解析json响应的结构:

@JsonObject
public class Image
{
    @JsonField(name = "_id")
    public int uuid;
    @JsonField
    public String img_url;
    @JsonField
    public String stylist;
    @JsonField
    public String address;
    @JsonField
    public String phone;
    @JsonField
    public String timetable;
    @JsonField
    public String latitude;
    @JsonField
    public String longitude;

    @JsonField
    private int privateInt;

    public int getPrivateInt() {
        return privateInt;
    }

    public void setPrivateInt(int i) {
        privateInt = i;
    }

    @OnJsonParseComplete
    void onParseComplete() {
        // Do some fancy post-processing stuff after parsing here
    }

    @OnPreJsonSerialize
    void onPreSerialize() {
        // Do some fancy pre-processing stuff before serializing here
    }
}

最后,我要求提供数据:

    AsyncHttpClient client = new AsyncHttpClient();

    client.get("http://www.privateurl.com/", new AsyncHttpResponseHandler() {

        @Override
        public void onStart() {
            // called before request is started
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
            // called when response HTTP status is "200 OK"

            String strResponse = new String(response);

            try {
                images = LoganSquare.parseList(strResponse, Image.class);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Toast toast = Toast.makeText(getApplicationContext(), String.valueOf(images.get(0).uuid), Toast.LENGTH_LONG);
            toast.show();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
            // called when response HTTP status is "4XX" (eg. 401, 403, 404)
        }

        @Override
        public void onRetry(int retryNo) {
            // called when request is retried
        }
    });
}

语句parseList不返回任何内容......我该如何解决?

1 个答案:

答案 0 :(得分:2)

您传递的课程与您正在接受的JSON不对应。

你有:

{ "version" : "", looks: [{},{},...]}

但是你的对象只适用于look数组。您的类应该具有版本属性JsonField和Image类的列表,然后它将被解析。

希望这有帮助。