I have a json string which is produced by a web service and I need to parse it and get values in it.
The json string looks like this:
{
"category": {
"Abnormal dress": [
{ "h": 44, "prob": 1.0, "w": 35, "x": 468, "y": 202 },
{ "h": 42, "prob": 0.9994152784347534, "w": 37, "x": 31, "y": 198 }
],
"Safety helmet": [ { "h": 60, "prob": 1.0, "w": 45, "x": 187, "y": 163 },
{ "h": 55, "prob": 1.0, "w": 44, "x": 394, "y": 157 },
{ "h": 60, "prob": 1.0, "w": 43, "x": 322, "y": 170 },
{ "h": 58, "prob": 1.0, "w": 52, "x": 536, "y": 156 },
{ "h": 26, "prob": 1.0, "w": 19, "x": 357, "y": 192 },
{ "h": 28, "prob": 1.0, "w": 36, "x": 31, "y": 176 },
{ "h": 13, "prob": 0.9999998807907104, "w": 12, "x": 357, "y": 140 }
]
},
"file_result": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKB9k=",
"status": "ok"
}
I defined my object classes as below:
package murraco.util;
import java.util.List;
public class FlaskRes {
String file_result;
String status;
Category category;//I have defined this field, why it cannot be recognized?
}
class Category{
List<Target> Abnormal_dress;
List<Target> Safety_helmet;
}
class Target{
double h;
double w;
double x;//left top
double y;
double prob;
}
I defined these classes because according to my knowledge, {}
in json should be mapping into an Java object, and []
should be an Array in java.
Then I use below code to parse the JSON string:
import com.fasterxml.jackson.databind.ObjectMapper;
...
resStr.replaceFirst("Abnormal dress","Abnormal_dress");
resStr.replaceFirst("Safety helmet","Safety_helmet");// to handle the whitespace
FlaskRes flaskRes=mapper.readValue(resStr, FlaskRes.class);
But I got below errors:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "category" (class murraco.util.FlaskRes), not marked as ignorable (0 known properties: ])
at [Source: (StringReader); line: 1, column: 17] (through reference chain: murraco.util.FlaskRes["category"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:855)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1212)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1604)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1582)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:299)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:156)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4524)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3466)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3434)
at murraco.util.FlaskServiceUtil.main(FlaskServiceUtil.java:33)