开发者

Parsing oddly constructed Json/Gson

I'll keep it brief, I'm having a problem with some really badly laid out Json (from what I can tell anyway), I'm pulling back from a web s开发者_JAVA百科ervice, I'm new to java/android/json so any help would be gratefully received, the structure of the Json is:

> {"cursor":
>         {"currentPageIndex":0,
>         "estimatedResultCount":32,
>                 "pages":{
>                          "label":1,
>                           "start":0},
>         "responseDetails":null,
>         "responseStatus":200}, "results":[
>         {"accuracy":24140,
>         "addressLines":
>                 {"Address1":"A house number",
>                 "Address2":"A Street",
>                 "Address3":"A city",
>                 "Address4":"A State",
>                 "Address5":"A Country",
>                 "Postcode":"123456"},
>         "city":"A City",
>         "country":"A Country",
>         "ddUrl":"http://link1.com",
>         "ddUrlFromHere":"http://link2.com",
>         "ddUrlToHere":"http://link3.com",
>         "lat":54.52909583,
>         "lng":-0.45554611,
>         "phoneNumbers":
>                 {"number":"123456789",
>                 "type":"work"},
>         "region":"China",
>         "staticMapUrl":"http://link4.com",
>         "streetAddress":"A house number, A Street, A city, A State,
> 123456",
>         "title":"Ali Baba Carpets",
>         "titleNoFormatting":"<strong>Ali Baba
> Carpets</strong>",
>         "url":"http://link5.com"}]}

I'm trying to parse it using:

import java.util.List;

public class ParseOffers {
    private List<results> results;

    public static class cursor {
        private int currentPageIndex;
        private int estimatedResultCount;
        private pages Pages;
        private String ResponseDetails;
        private int ResponseStatus;

        public class pages {
            private int label;
            private int start;
        }
    }

    public static class results {
        private int Accuracy;
        private addressLines AddressLines;
        private String city;
        private String country;
        private String ddUrl;
        private String ddUrlFromHere;
        private String ddUrlToHere;
        private double lat;
        private double lng;
        private phoneNumbers data;
        private String region;
        private String staticMapUrl;
        private String streetAddress;
        private String title;
        private String titleNoFormatting;
        private String url;

        public class addressLines {
            private String Address1;
            private String Address2;
            private String Address3;
            private String Address4;
            private String Address5;
            private String Postcode;
        }

        public class phoneNumbers {
            private String number;
            private String type;
        }

    }
}

Thanks.


The issue is simply that the Java data structure does not match the JSON, in a few different ways, including missing a field for cursor, and having field names that did not match the JSON element names.

Here is a minimally modified version of the Java data structure that matches the JSON.

public class ParseOffers
{
  private cursor cursor;
  private List<results> results;

  public static class cursor
  {
    private int currentPageIndex;
    private int estimatedResultCount;
    private pages pages;
    private String responseDetails;
    private int responseStatus;

    public class pages
    {
      private int label;
      private int start;
    }
  }

  public static class results
  {
    private int accuracy;
    private addressLines addressLines;
    private String city;
    private String country;
    private String ddUrl;
    private String ddUrlFromHere;
    private String ddUrlToHere;
    private double lat;
    private double lng;
    private phoneNumbers phoneNumbers;
    private String region;
    private String staticMapUrl;
    private String streetAddress;
    private String title;
    private String titleNoFormatting;
    private String url;

    public class addressLines
    {
      private String Address1;
      private String Address2;
      private String Address3;
      private String Address4;
      private String Address5;
      private String Postcode;
    }

    public class phoneNumbers
    {
      private String number;
      private String type;
    }

  }
}

While this Java data structure works, it still has oddities I'd change.

It's odd that some of the nested classes are static and some are not. I'd change it to not have any nested classes.

It's odd that some of the class names exactly match the field (and JSON element) names. I'd use more common naming conventions. For example, I'd change the name of the "cursor" class to "Cursor".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜