Java Gson http响应难以解析 [英] Java Gson http response difficult parsing

查看:344
本文介绍了Java Gson http响应难以解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析来自服务器的http json响应的大响应。创建响应文件的类是有害的,因为文件太大。我尝试过使用gson,但没有任何效果

这里是响应 http://www.sendspace.pl/file/ff7257d27380cf5e0c67a33



和我的代码:

  try {
JsonReader reader = new JsonReader(content);
JsonElement json = new JsonParser()。parse(reader);
reader.beginObject();
while(reader.hasNext()){
String name = reader.nextName();
if(name.equals(devices)){
System.out.println(gfdd);
} else {
reader.skipValue(); //避免一些未处理的事件
}
}
reader.endObject();
reader.close();
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

我收到异常:

 线程main中的异常java.lang.IllegalStateException:预期的BEGIN_OBJECT,但在第799行的第2列处为END_DOCUMENT,在com.google.gson.stream.JsonReader处为
。 expect(JsonReader.java:339)
在com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
在LightsControl.main(LightsControl.java:41)


解决方案

为了解析GSON的JSON响应,您需要创建一个



在您的情况下,您应该有如下所示的内容:

  public class Response {

@SerializedName(devices)
private List< Device>设备;
@SerializedName(场景)
私人列表< Scene>场景;
@SerializedName(sections)
private List< Section>段;
//...其他字段...

// getter和setter
}

然后:

  public class Device {

@SerializedName(id)
私人字符串ID;
@SerializedName(name)
私人字符串名称;
@SerializedName(device_type)
private String deviceType;
@SerializedName(device_file)
private String deviceFile;
@SerializedName(州)
私人列表<州>状态;
// ...其他字段

// getters和setters
}

然后您必须对类 Scene , Section State 等等...
显然它有点乏味,因为你的JSON响应很长,有很多不同的值...

好处是,如果您不需要它们,您可以轻松跳过值。例如,如果您不需要检索设备的状态,则可以删除属性 states 从类设备和GSON自动跳过这些值...



然后你只需用你的Reponse解析JSON对象,如下所示:

 字符串jsonString =您的json数据...; 
Gson gson = new Gson();
响应响应= gson.fromJson(jsonString,Response.class);


I want to parse big response from http json response from server. Creating class responding to file is harmful because file is too large. I tried with gson, but with no effect

Here is response http://www.sendspace.pl/file/ff7257d27380cf5e0c67a33

And my code :

try {
  JsonReader reader = new JsonReader(content);
  JsonElement json = new JsonParser().parse(reader);
  reader.beginObject();
  while (reader.hasNext()) {
    String name = reader.nextName();
    if (name.equals("devices")) {
      System.out.println("gfdd");
    } else {
      reader.skipValue(); //avoid some unhandle events
    }
  }
  reader.endObject();
  reader.close();
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

I am getting exception:

Exception in thread "main" java.lang.IllegalStateException: Expected BEGIN_OBJECT but was END_DOCUMENT at line 799 column 2
    at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
    at LightsControl.main(LightsControl.java:41)

解决方案

In order to parse your JSON response with GSON, you need to create a number of classes to wrap your reponse.

In your case, you should have something like:

public class Response {

  @SerializedName("devices")
  private List<Device> devices;
  @SerializedName("scenes")
  private List<Scene> scenes;
  @SerializedName("sections")
  private List<Section> sections;
  //...other fields...

  //getter and setter
}

And then:

public class Device{

  @SerializedName("id")
  private String id;
  @SerializedName("name")
  private String name;
  @SerializedName("device_type")
  private String deviceType;
  @SerializedName("device_file")
  private String deviceFile;
  @SerializedName("states")
  private List<State> states;
  //... other fields

  //getters and setters
}

And then you have to do the same with classes Scene, Section, State and so on... Obviously it's a little tedious because your JSON response is very long with many different values...

The good thing is that you can easily skip values if you don't need them. For example, if you don't need to retrieve the states of a device, you can just remove the attribute states from the class Device and GSON automatically skip those values...

Then you just parse the JSON with your Reponse object, like this:

String jsonString = "your json data...";
Gson gson = new Gson();
Response response = gson.fromJson(jsonString, Response.class);

这篇关于Java Gson http响应难以解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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