使用Jackson解析和未命名的数组 [英] Use Jackson to parse and unnamed array
问题描述
好的,所以我使用Jackson来解析一个JSON文件,问题是,该文件是一个大的未命名数组。它的格式为:
OK, so I'm using Jackson to parse a JSON file, problem is, the file is one big unnamed array. It is in the format:
[{json stuff},{json stuff},...,{json stuff}]
所有 json的东西
只是我必须处理的常规JSON表达式实际上把它变成一个数组。
All the json stuff
is just regular JSON expressions that I will have to deal with once I actually get this into an array.
我找不到任何有关如何使用Jackson映射事物的真实教程,但需要找到一种方法来映射这些不同的将事物分解为数组,然后使用Jackson将每个特定事物解析为其中的各个组件。知道怎么做吗?
I can't find any real tutorials about how to map things using Jackson, but need to find a way to map each of these different things into an array, and then parse each specific thing using Jackson into it's individual components. Any idea how to do this?
P.S。我能找到的唯一真正的教程是:
P.S. The only real tutorial I could find was this: http://www.studytrails.com/java/json/java-jackson-Data-Binding.jsp
其中DataSet []是在文件中命名的数组。我想弄清楚如何做该教程所做的事情,除了上面的例子,数组不以名字开头。
Where DataSet[] is an array that is named in the file. I want to figure out how to do what that tutorial does, except with the example above, where the array does not start off with a name.
P.P.S。这是我正在使用的代码:
P.P.S. Here is the code I'm using:
要映射到我的项目的基本杰克逊代码:
Basic Jackson code to map into my item:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
URL url = null;
try {
url = new URL("my JSON URL");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
ContactInfo contacts = mapper.readValue(url, ContactInfo.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
然后我的ContactInfo类只是Getters和Setters,其中包含已定义的字段JSON URL。问题是,没有分配所有不同JSON节点的数组的名称,我不知道如何访问单个联系人值,或者是否被覆盖。
Then my ContactInfo class is just the Getters and Setters with the defined fields in the JSON URL. The problem is, without a name to the Array that breaks up all the different JSON nodes, I don't know how to access individual contact values, or if they are being overwritten.
推荐答案
只需使用 ContactInfo []
作为类类型。这是一个工作示例。
Just use a ContactInfo[]
as the class type. Here's a working example.
public class Example {
public static void main(String[] args) throws Exception {
String json = "[{\"name\":\"random\"},{\"name\":\"random\"},{\"name\":\"random\"}]";
ObjectMapper mapper = new ObjectMapper();
ContactInfo[] contactInfos = mapper.readValue(json, ContactInfo[].class);
System.out.println(contactInfos.length);
}
static class ContactInfo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
或者,您可以使用列表< ContactInfo>
,但您需要 TypeReference
。
Alternatively, you can use a List<ContactInfo>
, but you'll need a TypeReference
.
List<ContactInfo> contactInfos = mapper.readValue(json, new TypeReference<List<ContactInfo>>() {});
System.out.println(contactInfos.size());
这篇关于使用Jackson解析和未命名的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!