简体   繁体   English

使用Gson解析没有名称的Json数组和对象

[英]Using Gson to parse Json array and object with no name

I know there are many JSON with GSON questions but none of them relate to me directly. 我知道有许多带有GSON问题的JSON,但它们都不直接与我有关。 My JSON is formatted differently. 我的JSON格式不同。

I have a JSON data I want to parse using GSON which looks like the following: 我有一个我想用GSON解析的JSON数据,如下所示:

[
   {
    "foo":"1",
    "bar":[ { "_id":"bar1"} ],
    "too":["mall", "park"]
   }
]

And I have the model Classes: 我有模型类:

ItemArray Class ItemArray类

public class ItemArray
{
   List<Item> itemArray;

   //Get set here
}

Item Class 物品等级

public class Item
{
   String foo;
   List<Bar> bar;
   List<String> too;

   //Get set here
}

Bar Class 酒吧类

public class Bar
{
   String id;

   //Get set here
}

Heres the question. 这是一个问题。 Is the JSON in the correct format? JSON的格式是否正确? If so, are the model classes in the correct format? 如果是这样,模型类的格式是否正确?

If not, please shove me in the right direction. 如果没有,请把我推向正确的方向。 Thank you in advance! 先感谢您!

PS. PS。 I can modify the JSON data format if need be. 如果需要,我可以修改JSON数据格式。

According to your json, you should simply have : 根据你的json,你应该简单地:

public class ItemArray extends List<Item> {
}

if you want to keep you java class and change your json it should be : 如果你想保留java类并更改你的json应该是:

{
 itemArray: [
     {
      "foo":"1",
      "bar":[ { "_id":"bar1"} ],
      "too":["mall", "park"]
     }
  ]
}

Oh, and there is a mismatch with the id and _id for Bar : 哦,和Bar的id和_id不匹配:

public class Bar
{
   String _id;

   //Get set here
}

You could also use an annotation to change the field's name during Json de/serialization. 您还可以使用注释在Json de / serialization期间更改字段的名称。

And last but not least, consider typing your values better. 最后但并非最不重要的是,考虑更好地输入您的值。 Don't see any data as strings if they are not, you will not a lot of processing in java code to convert things. 如果没有看到任何数据作为字符串,你不会在java代码中进行很多处理来转换东西。 For instance : 例如 :

"foo" : 1,

and see foo as an int data member, not a String. 并将foo视为int数据成员,而不是String。

Some times we get JsonArray [ {..} , {..} ] as a response (without 'itemArray' name like yours) In that case you can use following code 有时我们得到JsonArray [ {..} , {..} ]作为回应(没有像你的'itemArray'这样'itemArray'名字)在这种情况下你可以使用下面的代码

Type fooType = new TypeToken<ArrayList<Item>>() {}.getType();
List<Item> array = new Gson().fromJson(response, fooType);

find more about this Official doc - Gson Array-Examples 了解更多关于此官方文档 - Gson数组 - 示例

如果你有一个类似[ {..} , {..} ]JsonArray [ {..} , {..} ]你可以用Gson做到这一点:

Item[] items = gson.fromJson(json, Item[].class);

To check Json is valid use this tool http://jsonlint.com/ 要检查Json是否有效,请使用此工具http://jsonlint.com/

Class Bar(
   private String _id;
   //create getter/setters
{

public class Item
{
   String foo;
   List<Bar> bar;
   List<String> too;

   //Get set here
}
//this is also fine
public class ItemList
{
   List<Item> itemArray;

   //Get set here
}

you named of list of items "itemArray", but in your json you have not named the corresponding array of items "itemArray". 你命名的项目列表“itemArray”,但在你的json中你还没有命名相应的项目数组“itemArray”。 So make it itemArray, The problem is not in your json, it is valid. 所以把它变成itemArray,问题不在你的json中,它是有效的。 Problem is in its representation for Gson, Gson map keys of json on the variables of object (ie Java POJO) with same name. 问题在于其对Gson的Gson映射关键字,对象的变量(即Java POJO)具有相同的名称。 If the name of your list is Class is 如果列表的名称是Class is

List<Item> itemArray;

then the corresponding json array name should also be itemArray, take a look blow 那么相应的json数组名也应该是itemArray,看一下

{
 itemArray: [
     {
      "foo":"1",
      "bar":[ { "_id":"bar1"} ],
      "too":["mall", "park"]
     }
  ]
}

so you can convert json into object like that 所以你可以将json转换成这样的对象

 Reader reader = new InputStreamReader(IOUtils.toInputStream(json_string));
 ItemList itemList = json.toObject(reader, ItemList.class);

Take a look into blow reference for more details https://stackoverflow.com/questions/13625206/how-to-parse-the-result-in-java/13625567#13625567 深入了解详细信息, 请访问https://stackoverflow.com/questions/13625206/how-to-parse-the-result-in-java/13625567#13625567

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM