[英]How to get unnamed property from Json file using Jackson?
I have a JSON
feed which I am trying to parse: 我有一个要解析的
JSON
feed:
{
"test": [
"5",
{
"data": "someData",
"number": "9"
},
{
"data": "someData",
"number": "9"
}
]
}
I am using Jackson
to parse the JSON
file for me and using annotations: 我正在使用
Jackson
来为我解析JSON
文件并使用注释:
private ArrayList<Test> test = new ArrayList<Test>();
/**
* @param test the test to set
*/
public void setTest(ArrayList<Test> test) {
this.test = test;
}
And my Test
class: 我的
Test
课:
class Test{
private String data= "";
private String number= "";
/**
* @return the data
*/
public String getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(String data) {
this.data= data;
}
/**
* @return the number
*/
public String getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(String number) {
this.number= number;
}
} }
If I remove the 5
from my JSON
, it works fine. 如果我从
JSON
删除5
,它将正常工作。 However the 5
will be in the feed so I need a way to parse it. 但是
5
将出现在提要中,因此我需要一种解析方法。 I'm completely new to jackson and annotations in general and after spending most of the day trying to figure this out without any luck, I need some help! 一般来说,我是杰克逊和注解的新手,在花了大部分时间试图弄清楚这一点之后,我需要一些帮助!
How can I ignore the "5" if its not named, I've tried creating a container class which holds a String and the ArrayList and passed that as a paramater to the setTest method but that didnt work either. 我如何忽略“ 5”(如果未命名),我尝试创建一个包含String和ArrayList的容器类,并将其作为参数传递给setTest方法,但该方法也不起作用。
Your Java Test class represents the object, {"data": "someData", "number": "9"}
, in the array. 您的Java Test类在数组中表示对象
{"data": "someData", "number": "9"}
。 You will need to define another Java class to map your Test JSON object, something like 您将需要定义另一个Java类来映射您的Test JSON对象,例如
class TopLevel {
private int count;
List<Test> testList = new ArrayList<Test>();
// ...
//setters and getters
}
You can write custom deserializer for this list: 您可以为此列表编写自定义解串器:
class TestListDeserializer extends JsonDeserializer<List<Test>> {
@Override
public List<Test> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
List<Test> result = new ArrayList<Test>();
while (jp.nextToken() != JsonToken.END_ARRAY) {
if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
result.add(jp.readValueAs(Test.class));
}
}
return result;
}
}
Now, you have to annotated test
property: 现在,您必须注释
test
属性:
@JsonDeserialize(using = TestListDeserializer.class)
private List<Test> test;
You can do it this way 你可以这样
String jsonStr ="{\"test\": [\"5\",{ \"data\": \"someData\", \"number\": \"9\"},
{ \"data\": \"someData\",\"number\": \"10\"}]}";
JSONObject jsonObject =new JSONObject(jsonStr);
String jsonArr = jsonObject.get("test").toString();
JSONArray jsonArray = new JSONArray(jsonArr);
for(int i =1 ;i<jsonArray.length();i++){
JSONObject object = new JSONObject(jsonArray.get(i).toString());
System.out.println(object.get("data")); // use setter instead
System.out.println(object.get("number")); // use setter instead
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.