这个JSON的数据结构是什么? [英] What is the data structure of this JSON?

查看:89
本文介绍了这个JSON的数据结构是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用Gson解析Json到Java,但是当我使用fromJson()时,我总是得到null。谁能为我解释这个数据结构?

$b$ {
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,
标题:比尔盖茨官方网站 - 盖茨便笺,
描述:在商业和政府之间,即使是小投资也会对生活产生重大影响。 ,
DisplayUrl:www.thegatesnotes.com,
Url:http://www.thegatesnotes.com/
},
{
__metadata:{$ b $b/ u gates'& $ skip = 1& $ top = 1,
type:WebResult
},
ID:fdf0d3b9-b29f-43ef-b5ba-6bb4b1b04458,
标题:比尔盖茨 - 维基百科,自由的百科全书,
描述:威廉亨利\\比尔\\盖茨三世(生于1955年10月28日)是美国商业巨头和慈善家。盖茨是前首席执行官兼现任董事长......,
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
}

}



我认为数据结构应该是这样的,但它不起作用。

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

public class result {
公共信息[] infolist;}

公共类信息{
public __metadata元数据;
公共字符串ID;
公共字符串标题;
public String描述;
public String DisplayUrl;
public String Url;}

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

解决方案

您的信息类是问题所在。将信息填入结果中,并从 infolist 中移除结果。此外,元数据的字段名称是 __ metadata 。这不是课程名称。最后,你错过了一个类来包装 d 作为一个字段。

  public class DataContainer {
public Data d;
}

公共类数据{
public Result []结果;
public String __next;
}

公共类结果{
公共元数据__metadata;
公共字符串ID;
public String Title;
public String Description;
public String DisplayUrl;
public String Url;
}

公共类元数据{
public String uri;
public String type;
}

你真的应该为类名使用通用约定。 Gson不会排除你将自己的名字用于课程。它只需要控制字段的名称。



反序列化:

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

试试看看是否有效。

<




  • 打开<$ c $>开始时,您应该如何解释JSON c> {表示一个对象,所以这将是一个新类(或者一个现有的类,如果它们具有相同的字段)

  • this:表示其内部对象的字段,并且该字段必须与字符串中的文本命名为相同的内容。

  • [表示一个数组,一个 List 或一个 Set Result [] results )可以很容易地被 List< Result>结果


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;}

解决方案

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)

这篇关于这个JSON的数据结构是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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