简体   繁体   English

解析json时结果错误

[英]Wrong result while parsing json

I'm new to JSonObject, i need to decode a JSON String like this:我是 JSonObject 的新手,我需要像这样解码一个 JSON 字符串:

{
  "detected_faces": [
    {
      "BoundingBox": {
        "startX": 9,
        "startY": 0,
        "endX": 1024,
        "endY": 1024,
        "Probability": 98.90444278717041
      },
      "Gender": {
        "Gender": "female",
        "Probability": 73.33024740219116
      },
      "Age": {
        "Age-Range": {
          "Low": 39,
          "High": 53
        }
      }
    }
  ]
}

I created class:我创建了类:

 class BoundingBox{
    public int startX;
    public int startY;
    public int endX;
    public int endY;
    @JsonProperty("Probability")
    public String probability;
}

 class Gender{
    @JsonProperty("Gender") 
    public String gender;
    @JsonProperty("Probability") 
    public double probability;
}

 class AgeRange{
    @JsonProperty("Low") 
    public int low;
    @JsonProperty("High") 
    public int high;
}

 class Age{
    @JsonProperty("Age-Range") 
    public AgeRange ageRange;
}

 class DetectedFace{
    @JsonProperty("BoundingBox") 
    public BoundingBox boundingBox;
    @JsonProperty("Gender") 
    public Gender gender;
    @JsonProperty("Age") 
    public Age age;
}

but if I map to DetectedFace, all field are null.但如果我映射到 DetectedFace,所有字段都为空。

so I tried with this code:所以我尝试使用此代码:

JsonArray arr = jsonObject.getAsJsonArray("detected_faces");
JsonObject jp = new JsonParser().parse(arr.get(0).toString()).getAsJsonObject();
Gson gs = new Gson();
JsonObject jbb = jp.getAsJsonObject("BoundingBox");
BoundingBox bb = gs.fromJson(jbb,BoundingBox.class);
JsonObject jgn = jp.getAsJsonObject("Gender");
Gender gn = gs.fromJson(jgn,Gender.class);

int fields are correctly filled, doubles result to 0 and strings are null. int 字段被正确填充,双精度结果为 0,字符串为空。 Can anyone tell me where are my mistakes?谁能告诉我我的错误在哪里?

I think you don't need to mix Gson and Jackson to deserialize json.我认为你不需要混合 Gson 和 Jackson 来反序列化 json。 Just choose one of them.只需选择其中之一。

This code uses only Jackson.此代码仅使用 Jackson。

ObjectMapper mapper = new ObjectMapper();
OuterObject outerObject = mapper.readValue(text, OuterObject.class);

DetectedFace detectedFace = outerObject.detectedFaces.get(0);
BoundingBox boundingBox = detectedFace.boundingBox;
Gender gender = detectedFace.gender;
Age age = detectedFace.age;
class OuterObject{
    @JsonProperty("detected_faces")
    public List<DetectedFace> detectedFaces;
}

Sample result:示例结果:

OuterObject(detectedFaces=[DetectedFace(boundingBox=BoundingBox(startX=9,
 startY=0, endX=1024, endY=1024, probability=98.90444278717041), 
gender=Gender(gender=female, probability=73.33024740219116), 
age=Age(ageRange=AgeRange(low=39, high=53)))])

try this,尝试这个,

public static <T> ArrayList<T> getArrayListFromJSON(String dataJSON) {
    TypeToken<ArrayList<T>> token = new TypeToken<ArrayList<T>>() {
    };
    ArrayList<T> dataList = new Gson().fromJson(dataJSON, token.getType());
    Log.d(TAG, "getArrayListFromJSON:  dataList : " + dataList);
    return dataList;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM