解析JSON阵列GSON [英] Parsing JSON array with gson

查看:247
本文介绍了解析JSON阵列GSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解析我的JSON,我从JavaScript得到。
JSON格式是这样的:

<$p$p><$c$c>[{\"positions\":[{\"x\":50,\"y\":50},{\"x\":82,\"y\":50},{\"x\":114,\"y\":50},{\"x\":146,\"y\":50}]},{\"positions\":[{\"x\":210,\"y\":50},{\"x\":242,\"y\":50},{\"x\":274,\"y\":50}]}]

到目前为止,我已经能够走到这一步:

  {阵地:[{X:50,Y:50},{X:82,Y:50},{X :114,Y:50},{×:146,Y:50}]}

但我也需要立即创建一类具有这些职位。我havnt一直工作在类,因为我第一次尝试打印出的输出,但我无法进一步分解。我收到此错误信息:

java.lang.IllegalStateException:这不是一个JSON阵列

和我的code是这样的:

  JsonParser分析器=新JsonParser();
    字符串船舶=的request.getParameter(JSONships);
    JsonArray阵列= parser.parse(船舶).getAsJsonArray();    的System.out.println(array.get(0)的ToString());
    JsonArray数组2 = parser.parse(array.get(0)的ToString())getAsJsonArray();
    的System.out.println(array2.get(0)的ToString());

我也试图做这种方式:

  GSON GSON =新GSON();
    串洛尔=(gson.fromJson(array.get(0),为String.class));
    的System.out.println(笑);

在这种情况下,我得到:

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期STRING但BEGIN_OBJECT

在最后,我想通过位置循环,为每一个阵地,其中包含与其他类职位,其中有整型的x,y列表创建类。

感谢您的时间。


解决方案

定义你的类,你会得到你需要使用GSON一切:

 公共类的Class1 {
  私人诠释X;
  私人列表&LT;&Class2中GT;要素;
}

和内部类:

 公共类的Class2 {
  私人字符串STR1;
  私人整数INT2;
}

现在可以解析外部类就是这样的一个JSON字符串:

  gson.fromJson(jsonString,Class1.class);

您的错误,而使用GSON是,你尝试在字符串,这是不可能的解析一个复杂的对象。

I am having trouble parsing my JSON which i get from javascript. The format of JSON is this:

[{"positions":[{"x":50,"y":50},{"x":82,"y":50},{"x":114,"y":50},{"x":146,"y":50}]},{"positions":[{"x":210,"y":50},{"x":242,"y":50},{"x":274,"y":50}]}]

So far i have been able to get this far:

{"positions":[{"x":50,"y":50},{"x":82,"y":50},{"x":114,"y":50},{"x":146,"y":50}]}

But i also need to now create a class with those positions. I havnt been working on the class, since i tried printing out the output first, but i am unable to break it down further. I am getting this error message:

java.lang.IllegalStateException: This is not a JSON Array.

And my code is this:

    JsonParser parser = new JsonParser();
    String ships = request.getParameter("JSONships");
    JsonArray array = parser.parse(ships).getAsJsonArray();

    System.out.println(array.get(0).toString());
    JsonArray array2 = parser.parse(array.get(0).toString()).getAsJsonArray();
    System.out.println(array2.get(0).toString());

I have also tried to do it this way:

    Gson gson = new Gson() ;
    String lol = (gson.fromJson(array.get(0), String.class));
    System.out.println(lol);

In which case i get:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected STRING but was BEGIN_OBJECT

In the end, i want to loop through positions, creating class for each "positions", which contains a List with another class Position, which has the int x, y.

Thank you for your time.

解决方案

Define your classes and you will get everything you need using gson:

public class Class1 {
  private int x;
  private List<Class2> elements;
}

And the inner class:

public class Class2 {
  private String str1;
  private Integer int2;
}

Now you can parse a json string of the outer class just like that:

gson.fromJson(jsonString, Class1.class);

Your error while using Gson is that you try to parse a complex object in String, which is not possible.

这篇关于解析JSON阵列GSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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