Gson 和 argonauts - 使用 gson 将 javascript 数组转换为 json 字符串、转换为 java pojo 时出现问题。试图让我的结构正确

发布于 2024-09-17 12:15:07 字数 2522 浏览 5 评论 0原文

这是我在 java 中访问的 json 字符串:

json = 
[
    {"id":"123456","Data":"skill2","dmlcrud":false},
    {"id":"123456","Data":"skill3","dmlcrud":true},
    {"id":"123456","Data":"skill14","dmlcrud":true},
    {"id":"123321","Data":"skill1","dmlcrud":false},
    {"id":"123321","Data":"skill14","dmlcrud":false}
]

我现在想将它放入一个集合中,所以理想/理论上我想要这样做:

List<Person> personList = new Gson().fromJson(json, Person.class);

并且 personList.size() = 5。然后我将循环 personList 和 preform我的相关行动。

但是,我的理解是我需要创建一个容器类,它本身包含人员列表?因此,而不是(为了简洁而删除了公共 getters/setters,可能还有语法错误)。

Class Person {
   private integer id;
   private String Data;
   private Boolean dmlCrud ;
}

我实际上需要类似的东西?

Class container{
   List<Person> personList;


   static class Person {
      private integer id;
      private String Data;
      private Boolean dmlCrud ;
   }
}

但是我还需要将 javascript json 更改为不同的东西吗?这看起来相当有问题,因为我使用 JSON.stringifier 从 javascript 数组创建 json 字符串。

非常感谢任何帮助。

编辑

我使用的解决方案是添加

public List<Person> personList;

到 person 类中 并更改 json 对象,以便它是

{ "personList" :
    [
        {"id":"123456","Data":"skill2","dmlcrud":false},
        {"id":"123456","Data":"skill3","dmlcrud":true},
        {"id":"123456","Data":"skill14","dmlcrud":true},
        {"id":"123321","Data":"skill1","dmlcrud":false},
        {"id":"123321","Data":"skill14","dmlcrud":false}
    ]
}

gson 调用

Person person = new Gson().fromJson(json, Person.class);

,并且可以在列表中访问数据,如下

List<Person> personList = person.getPersonList();

编辑 2

第二个更好的解决方案是使用此 json 数组

[
        {"id":"123456","Data":"skill2","dmlcrud":false},
        {"id":"123456","Data":"skill3","dmlcrud":true},
        {"id":"123456","Data":"skill14","dmlcrud":true},
        {"id":"123321","Data":"skill1","dmlcrud":false},
        {"id":"123321","Data":"skill14","dmlcrud":false}
]

然后使用

Type listType = new TypeToken<List<SkillsGsonTO>>() {}.getType();
List<Person> personList  = new Gson().fromJson(json,listType);
Person person1 = personList.get(0);

, 使用原始类

Class Person {
       private integer id;
       private String Data;
       private Boolean dmlCrud ;
    }

Here is my json string, that I am acessing in java:

json = 
[
    {"id":"123456","Data":"skill2","dmlcrud":false},
    {"id":"123456","Data":"skill3","dmlcrud":true},
    {"id":"123456","Data":"skill14","dmlcrud":true},
    {"id":"123321","Data":"skill1","dmlcrud":false},
    {"id":"123321","Data":"skill14","dmlcrud":false}
]

I now want to put it in a collection so ideally/theoretically I would want to do:

List<Person> personList = new Gson().fromJson(json, Person.class);

and personList.size() would = 5. I would then loop through personList and preform my relevant actions.

However, my understanding is that I would need to create a container class, which itself contains the person list ? So instead of (public getters/setters removed for brevity, probably syntax errror in there aswell).

Class Person {
   private integer id;
   private String Data;
   private Boolean dmlCrud ;
}

I would actually need something like ?

Class container{
   List<Person> personList;


   static class Person {
      private integer id;
      private String Data;
      private Boolean dmlCrud ;
   }
}

However I would then need to alter the javascript json to be somethign different aswell ? Which seems rather problematic as am I creating the json string from a javascript array, using JSON.stringifier.

Any help gratefully received.

EDIT

the solution I used was to add

public List<Person> personList;

to the person class
and alter the json object so that it was

{ "personList" :
    [
        {"id":"123456","Data":"skill2","dmlcrud":false},
        {"id":"123456","Data":"skill3","dmlcrud":true},
        {"id":"123456","Data":"skill14","dmlcrud":true},
        {"id":"123321","Data":"skill1","dmlcrud":false},
        {"id":"123321","Data":"skill14","dmlcrud":false}
    ]
}

the gson call can then be

Person person = new Gson().fromJson(json, Person.class);

and the data accessed in a list like so

List<Person> personList = person.getPersonList();

EDIT 2

A second, better, solution is to use this json array

[
        {"id":"123456","Data":"skill2","dmlcrud":false},
        {"id":"123456","Data":"skill3","dmlcrud":true},
        {"id":"123456","Data":"skill14","dmlcrud":true},
        {"id":"123321","Data":"skill1","dmlcrud":false},
        {"id":"123321","Data":"skill14","dmlcrud":false}
]

and then use

Type listType = new TypeToken<List<SkillsGsonTO>>() {}.getType();
List<Person> personList  = new Gson().fromJson(json,listType);
Person person1 = personList.get(0);

where the original class is used

Class Person {
       private integer id;
       private String Data;
       private Boolean dmlCrud ;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-09-24 12:15:25

您可以使用 Container 类,但这仅在您需要在人员列表上发送其他属性时才有意义。如果不是这种情况,您也可以转换为 java.util.List。我认为您需要将列表属性的“名称”指定为 JSON 字符串中的根元素。例如,如果您的域对象是 Person 对象列表,那么您的 JSON 根元素是:“persons”或“personList”。所以你的 JSON 可能看起来像这样:

"persons" : {[
     {"id":"123456","Data":"skill2","dmlcrud":false},
     {"id":"123456","Data":"skill3","dmlcrud":true},
     {"id":"123456","Data":"skill14","dmlcrud":true},
     {"id":"123321","Data":"skill1","dmlcrud":false},
     {"id":"123321","Data":"skill14","dmlcrud":false}
]}

我的语法可能有点偏离,但它应该与此类似。总结一下:

在您的情况下,您可以保持 Person 类不变,并且 gson 应该能够从我建议的 JSON 字符串为您创建列表人员。

来自 Gson API 文档:
如果您要反序列化的对象是 ParameterizedType(即至少包含一个类型参数并且可能是一个数组),那么您必须使用 fromJson(String, Type) 方法。这是反序列化 ParameterizedType 的示例:

 Type listType = new TypeToken<List<String>>() {}.getType();

 Gson gson = new Gson();
 List<String> target2 = gson.fromJson(json, listType);

所以在您的情况下它将是:

Type listType = new TypeToken<List<Person>>() {}.getType();
List<Person> persons = new Gson().fromJson(json, listType);

其中 json 显然是您的 json 字符串

You could use a Container class but this only makes sense if you need to ship additional properties on the person list. If this is not the case, you could convert to a java.util.List as well. I think you need to specify the "name" of the list property as a root element in your JSON string. So for instance if you're domain object is a List of Person objects, than your JSON root element is: "persons" or "personList". So you're JSON could look something like:

"persons" : {[
     {"id":"123456","Data":"skill2","dmlcrud":false},
     {"id":"123456","Data":"skill3","dmlcrud":true},
     {"id":"123456","Data":"skill14","dmlcrud":true},
     {"id":"123321","Data":"skill1","dmlcrud":false},
     {"id":"123321","Data":"skill14","dmlcrud":false}
]}

I could be a little bit off with the syntax, but it should be something similar to this. So to summarize:

In your case you can leave you're Person class untouched and gson should be able to create the List persons for you from the JSON string I suggested.

From the Gson API docs:
If the object that your are deserializing is a ParameterizedType (i.e. contains at least one type parameter and may be an array) then you must use the fromJson(String, Type) method. Here is an example for deserialing a ParameterizedType:

 Type listType = new TypeToken<List<String>>() {}.getType();

 Gson gson = new Gson();
 List<String> target2 = gson.fromJson(json, listType);

So in your case it would be:

Type listType = new TypeToken<List<Person>>() {}.getType();
List<Person> persons = new Gson().fromJson(json, listType);

where json is your json string obviously

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文