将 JSON 数据转换为 Java 对象 [英] Converting JSON data to Java object
问题描述
我希望能够从我的 Java 操作方法中的 JSON 字符串访问属性.只需说 myJsonString = object.getJson()
即可获得该字符串.以下是字符串外观的示例:
I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson()
. Below is an example of what the string can look like:
{
'title': 'ComputingandInformationsystems',
'id': 1,
'children': 'true',
'groups': [{
'title': 'LeveloneCIS',
'id': 2,
'children': 'true',
'groups': [{
'title': 'IntroToComputingandInternet',
'id': 3,
'children': 'false',
'groups': []
}]
}]
}
在这个字符串中,每个 JSON 对象都包含其他 JSON 对象的数组.目的是提取 ID 列表,其中任何给定对象拥有包含其他 JSON 对象的组属性.我将 Google 的 Gson 视为一个潜在的 JSON 插件.任何人都可以就如何从这个 JSON 字符串生成 Java 提供某种形式的指导吗?
In this string every JSON object contains an array of other JSON objects. The intention is to extract a list of IDs where any given object possessing a group property that contains other JSON objects. I looked at Google's Gson as a potential JSON plugin. Can anyone offer some form of guidance as to how I can generate Java from this JSON string?
推荐答案
我将 Google 的 Gson 视为潜在的 JSON 插件.任何人都可以就如何从这个 JSON 字符串生成 Java 提供某种形式的指导吗?
Google Gson 支持泛型和嵌套 bean.JSON 中的 []
表示一个数组,应该映射到一个 Java 集合,例如 List
或只是一个普通的 Java 数组.JSON 中的 {}
表示一个对象,应该映射到 Java Map
或只是一些 JavaBean 类.
Google Gson supports generics and nested beans. The []
in JSON represents an array and should map to a Java collection such as List
or just a plain Java array. The {}
in JSON represents an object and should map to a Java Map
or just some JavaBean class.
您有一个具有多个属性的 JSON 对象,其中 groups
属性表示一组相同类型的嵌套对象.这可以通过以下方式用 Gson 解析:
You have a JSON object with several properties of which the groups
property represents an array of nested objects of the very same type. This can be parsed with Gson the following way:
package com.stackoverflow.q1688099;
import java.util.List;
import com.google.gson.Gson;
public class Test {
public static void main(String... args) throws Exception {
String json =
"{"
+ "'title': 'Computing and Information systems',"
+ "'id' : 1,"
+ "'children' : 'true',"
+ "'groups' : [{"
+ "'title' : 'Level one CIS',"
+ "'id' : 2,"
+ "'children' : 'true',"
+ "'groups' : [{"
+ "'title' : 'Intro To Computing and Internet',"
+ "'id' : 3,"
+ "'children': 'false',"
+ "'groups':[]"
+ "}]"
+ "}]"
+ "}";
// Now do the magic.
Data data = new Gson().fromJson(json, Data.class);
// Show it.
System.out.println(data);
}
}
class Data {
private String title;
private Long id;
private Boolean children;
private List<Data> groups;
public String getTitle() { return title; }
public Long getId() { return id; }
public Boolean getChildren() { return children; }
public List<Data> getGroups() { return groups; }
public void setTitle(String title) { this.title = title; }
public void setId(Long id) { this.id = id; }
public void setChildren(Boolean children) { this.children = children; }
public void setGroups(List<Data> groups) { this.groups = groups; }
public String toString() {
return String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups);
}
}
相当简单,不是吗?只要有一个合适的 JavaBean 并调用 Gson#fromJson()
.
Fairly simple, isn't it? Just have a suitable JavaBean and call Gson#fromJson()
.
- Json.org - Introduction to JSON
- Gson User Guide - Introduction to Gson
这篇关于将 JSON 数据转换为 Java 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!