What is the data structure of this JSON?

谁说胖子不能爱 提交于 2020-01-04 04:09:17

问题


I'm trying to parse Json to Java by using Gson, but when I use fromJson(), I always get null. Who can explain this data structure for me? Thanks!

{
"d": {
    "results": [
        {
            "__metadata": {
                "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query='bill'gates'&$skip=0&$top=1",
                "type": "WebResult"
            },
            "ID": "9bd0942f-fe5b-44fc-8343-ef85e5b93a7e",
            "Title": "The Official Site of Bill Gates - The Gates Notes",
            "Description": "In the space between business and goverment, even a small investment can make a big impact on the lives of those in need.",
            "DisplayUrl": "www.thegatesnotes.com",
            "Url": "http://www.thegatesnotes.com/"
        },
        {
            "__metadata": {
                "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query='bill'gates'&$skip=1&$top=1",
                "type": "WebResult"
            },
            "ID": "fdf0d3b9-b29f-43ef-b5ba-6bb4b1b04458",
            "Title": "Bill Gates - Wikipedia, the free encyclopedia",
            "Description": "William Henry \"Bill\" Gates III (born October 28, 1955) is an American business magnate and philanthropist. Gates is the former chief executive and current chairman of ...",
            "DisplayUrl": "en.wikipedia.org/wiki/Bill_Gates",
            "Url": "http://en.wikipedia.org/wiki/Bill_Gates"
        }
    ],
    "__next": "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query='bill'gates'&$skip=10&$top=10"
}

}

I think the data structure should be like this, but it doesn't work.

public class d {
  public result[] results;
  public String __next;}

public class result {
  public information[] infolist;}

public class information {
  public  __metadata metadata;
  public String ID;
  public String Title;
  public String Description;
  public String DisplayUrl;
  public String Url;}

public class __metadata {
   public String uri;
   public String type;}

回答1:


Your Information class is the problem. Put the Information stuff into Result and remove the infolist from Result. Also, the field name for the meta data is __metadata. This isn't the class name. Lastly, you're missing a class to wrap d as a field.

public class DataContainer {
    public Data d;
}

public class Data {
    public Result[] results;
    public String __next;
}

public class Result {
    public Metadata __metadata;
    public String ID;
    public String Title;
    public String Description;
    public String DisplayUrl;
    public String Url;
}

public class Metadata {
    public String uri;
    public String type;
}

You really should use common convention for class names. Gson won't preclude you from using your own names for classes. It only requires control for the name of the fields.

To deserialize:

String json = ... ;
DataContainer myDataContainer = new Gson().fromJson(JSONString , DataContainer.class);
Result[] myResult = myDataContainer.d.results;

Try that and see if that works.

Here's how you should interpret the JSON when you're writing a class structure around it for Gson:

  • An opening { indicates an object, so this will be a new class (or an existing one if they have the same fields)
  • A "this": indicates a field for the object it's inside, and the field must be named the same thing as the text in the string.
  • An opening [ indicates an array, a List, or a Set (Result[] results could just as easily be List<Result> results)


来源:https://stackoverflow.com/questions/12630054/what-is-the-data-structure-of-this-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!