不同的JSON数组响应 [英] Different JSON array response

查看:140
本文介绍了不同的JSON数组响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



1:这是我从RESTful API获得的JSON响应:

  {
gear:[
{
idGear:1,
name: Nosilec za kolesa,
year:2005,
price:777.0
},{
idGear:2,
name:Stresni nosilci,
year:1983,
price:40.0
}
]
}



<2>:我从测试客户端获得的响应。我在列表中添加了一些值,然后使用 gson.toJson 作为测试输出。

  [
{
idGear:1,
name:lala,
year:2000,
price:15.0
},{
idGear:2,
name:lala2,
year:2000,
价格:125.0
}
]

它们都是有效的,但第二个被成功反序列化为像这样的对象:

 输入listType = new TypeToken< List< Gear>>(){ } .getType(); 
列表< Gear>齿轮=(List< Gear>)gson.fromJson(json,listType);

第一个,我试图以同样的方式反序列化,但是我得到错误。






编辑



API方法: / p>

  @GET 
@Produces(MediaType.APPLICATION_JSON)
public List< Gear> getGear(){
List< Gear> gears = gearDAO.getGears();
if(!gears.isEmpty()){
return gears;
} else
抛出新的RuntimeException(No gears);
}

客户端序列化代码:

 列表与LT;齿轮及GT; list = new ArrayList< Gear>(); 
Gear o =新Gear();
o.setPrice(15);
o.setYear(2000);
o.setName(asds);
键入listTypes = new TypeToken< List< Gear>>(){} .getType();
gson.toJson(list,listTypes);


解决方案

JSON响应不同!


  • 第一个是一个对象,由 {} 包围,其中包含gear字段,该字段依次为 []


  • 第二个是直接的对象列表,因为它被 [] 包围。即,整个第二个响应相当于第一个响应中的字段。




所以,显然他们不能以相同的方式解析...



第二个解析正确,因为您正在使用 List 这是一个列表。但是对于第一个,您需要另一个类,其中包含一个字段,该字段依次包含一个列表......也就是说,您只需创建一个表示您的JSON响应的类结构...

  public class Response {
private List< Gear>齿轮;
// getters& setters
}

现在您可以使用以下命令解析第一个响应:

  Gson gson = new Gson(); 
响应响应= gson.fromJson(json,Response .class);
列表< Gear> gears = response.getGears();






我建议你简单看一下<一个href =http://www.json.org/> json.org 为了理解JSON语法,这很简单...
基本上这些是可能的JSON元素:

  object 
{}
{members}
members

对,成员

字符串:值
数组
[]
[元素]
元素

值元素

字符串
编号
对象
数组
true
false
null


I have problems parsing two different JSON responses.

1: This is the JSON response I get from a RESTful API:

{
  "gear": [
    {
      "idGear": "1",
      "name": "Nosilec za kolesa",
      "year": "2005",
      "price": "777.0"
    }, {
      "idGear": "2",
      "name": "Stresni nosilci",
      "year": "1983",
      "price": "40.0"
    }
  ]
}

2: This response I get from my testing client. I was added some values to the list and then I used gson.toJson for testing output.

[
  {
    "idGear": "1",
    "name": "lala",
    "year": 2000,
    "price": 15.0
  }, {
    "idGear": "2",
    "name": "lala2",
    "year": 2000,
    "price": 125.0
  }
]

They are both valid, but the second one was successfully deserialize to object like this:

Type listType = new TypeToken<List<Gear>>() {}.getType();
List<Gear> gears= (List<Gear>) gson.fromJson(json, listType); 

With the first one, I was trying to deserialize the same way but I get error.


EDIT

API Method:

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Gear> getGear() {
  List<Gear> gears = gearDAO.getGears();
  if (!gears.isEmpty()) {
    return gears;
  } else
    throw new RuntimeException("No gears");
}

CLIENT serialization code:

List<Gear> list = new ArrayList<Gear>();
Gear o = new Gear();
o.setPrice(15);
o.setYear(2000);
o.setName("asds");
Type listTypes = new TypeToken<List<Gear>>() {}.getType();
gson.toJson(list, listTypes);

解决方案

The JSON responses are different!

  • The first one is an object, surrounded by { }, which contains a field "gear" that is in turn a list of objects, surrounded by [ ].

  • The second one is directly a list of objects, because it's surrounded by [ ]. Namely, the whole 2nd response is equivalent to the field in the 1st response.

So, obviously they can't be parsed in the same way...

The 2nd one is being parsed correctly because you are using a List and it is a list. But for the 1st one you need another class that contains a field that contains in turn a list... That is, you just need to create a class structure that represents your JSON responses...

public class Response {    
    private List<Gear> gears;        
    //getters & setters
}

Now you can parse your 1st response with:

Gson gson = new Gson();
Response response = gson.fromJson(json, Response .class);
List<Gear> gears = response.getGears();


I suggest you to take a brief look at json.org in order to understand JSON syntax, which is pretty simple... Basically these are the possible JSON elements:

object
    {}
    { members } 
members
    pair
    pair , members
pair
    string : value
array
    []
    [ elements ]
elements
    value
    value , elements
value
    string
    number
    object
    array
    true
    false
    null 

这篇关于不同的JSON数组响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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