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

时间:2022-09-10 13:37:56

I know there are many JSON with GSON questions but none of them relate to me directly. My JSON is formatted differently.

我知道有许多带有GSON问题的JSON,但它们都不直接与我有关。我的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? If so, are the model classes in the correct format?

这是一个问题。 JSON的格式是否正确?如果是这样,模型类的格式是否正确?

If not, please shove me in the right direction. Thank you in advance!

如果没有,请把我推向正确的方向。先谢谢你!

PS. I can modify the JSON data format if need be.

PS。如果需要,我可以修改JSON数据格式。

4 个解决方案

#1


6  

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 /序列化期间更改字段的名称。

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. For instance :

最后但并非最不重要的是,考虑更好地输入您的值。如果没有看到任何数据作为字符串,你不会在java代码中进行很多处理来转换东西。例如 :

"foo" : 1,

and see foo as an int data member, not a String.

并将foo视为int数据成员,而不是String。

#2


5  

Some times we get JsonArray [ {..} , {..} ] as a response (without 'itemArray' name like yours) In that case you can use following code

有时我们得到JsonArray [{..},{..}]作为响应(没有像你的'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数组 - 示例

#3


2  

If you have a JsonArray like [ {..} , {..} ] you can do this with Gson:

如果你有像[{..},{..}]这样的JsonArray,你可以用Gson做到这一点:

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

#4


0  

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". So make it itemArray, The problem is not in your json, it is valid. Problem is in its representation for Gson, Gson map keys of json on the variables of object (i.e Java POJO) with same name. If the name of your list is Class is

你命名的项目列表“itemArray”,但在你的json中你还没有命名相应的项目数组“itemArray”。所以把它变成itemArray,问题不在你的json中,它是有效的。问题在于它表示对于具有相同名称的对象变量(即Java POJO)的json的Gson,Gson映射键。如果列表的名称是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://*.com/questions/13625206/how-to-parse-the-result-in-java/13625567#13625567

深入了解详细信息,请访问https://*.com/questions/13625206/how-to-parse-the-result-in-java/13625567#13625567

#1


6  

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 /序列化期间更改字段的名称。

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. For instance :

最后但并非最不重要的是,考虑更好地输入您的值。如果没有看到任何数据作为字符串,你不会在java代码中进行很多处理来转换东西。例如 :

"foo" : 1,

and see foo as an int data member, not a String.

并将foo视为int数据成员,而不是String。

#2


5  

Some times we get JsonArray [ {..} , {..} ] as a response (without 'itemArray' name like yours) In that case you can use following code

有时我们得到JsonArray [{..},{..}]作为响应(没有像你的'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数组 - 示例

#3


2  

If you have a JsonArray like [ {..} , {..} ] you can do this with Gson:

如果你有像[{..},{..}]这样的JsonArray,你可以用Gson做到这一点:

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

#4


0  

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". So make it itemArray, The problem is not in your json, it is valid. Problem is in its representation for Gson, Gson map keys of json on the variables of object (i.e Java POJO) with same name. If the name of your list is Class is

你命名的项目列表“itemArray”,但在你的json中你还没有命名相应的项目数组“itemArray”。所以把它变成itemArray,问题不在你的json中,它是有效的。问题在于它表示对于具有相同名称的对象变量(即Java POJO)的json的Gson,Gson映射键。如果列表的名称是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://*.com/questions/13625206/how-to-parse-the-result-in-java/13625567#13625567

深入了解详细信息,请访问https://*.com/questions/13625206/how-to-parse-the-result-in-java/13625567#13625567