简体   繁体   English

在json对象中反序列化json对象

[英]Deserialize json object in json object

I am having a hard time deserializing json object, and json array from a json object. 我很难从json对象反序列化json对象和json数组。 I want to return query, intent, and entity. 我想返回查询,意图和实体。 I was able to get query already. 我已经能够获得查询了。 I want to get "intent" under "topScoringIntent" and "entity" under "entities" using gson and return all of them in a class. 我希望使用gson在“实体”下的“topScoringIntent”和“entity”下获得“intent”,并在类中返回所有这些内容。

                Scanner scanner = new Scanner(con.getInputStream()); 
                String responseBody = scanner.useDelimiter("\\A").next();
                System.out.println(responseBody);

                Gson gson = new GsonBuilder().create();
                LuisResult jsonsluisresult = gson.fromJson(responseBody, LuisResult.class);   
                System.out.println(jsonsluisresult.toString());
                response.setQuery(jsonsluisresult.getQuery());

                JSONObject topIntent = jsonsluisresult.getTopScoringIntent();
                JSONObject intent = topIntent.getJSONObject("score");

public class LuisResult {
    public LuisResult() {
        super();
    }

private String query;

public void setQuery(String query) {
    this.query = query;
}

public String getQuery() {
    return query;
}

private JSONObject topScoringIntent;

public void setTopScoringIntent(JSONObject topScoringIntent) {
    this.topScoringIntent = topScoringIntent;
}

public JSONObject getTopScoringIntent() {
    return topScoringIntent;
}


private ArrayList<JSONObject> entities;

public void setEntities(ArrayList<JSONObject> entities) {
    this.entities = entities;
}

public ArrayList<JSONObject> getEntities() {
    return entities;
}


private String intent;

public void setIntent(String intent) {
    this.intent = intent;
}

public String getIntent() {
    return intent;
}

}

responseBody: responseBody:

{
  "query": "what is the weather like in texas",
  "topScoringIntent": {
    "intent": "GetWeather",
    "score": 0.697563648
  },
  "entities": [
    {
      "entity": "texas",
      "type": "Location",
      "startIndex": 28,
      "endIndex": 32,
      "score": 0.693443358
    }
  ]
}

I want to get "intent" under "topScoringIntent" and "entity" under "entities" using gson and return all of them in a class. 我希望使用gson在“实体”下的“topScoringIntent”和“entity”下获得“intent”,并在类中返回所有这些内容。

Instead of using JsonObject , you can use typed POJOs like these: 您可以使用类似这样的类型化POJO,而不是使用JsonObject

class Result {
    private String query;
    private TopScoringIntent topScoringIntent;
    private List<Entity> entities;

    public List<Entity> getEntities() {
        return entities;
    }

    public void setEntities(List<Entity> entities) {
        this.entities = entities;
    }

    public String getQuery() {
        return query;
    }
    public void setQuery(String query) {
        this.query = query;
    }
    public TopScoringIntent getTopScoringIntent() {
        return topScoringIntent;
    }
    public void setTopScoringIntent(TopScoringIntent topScoringIntent) {
        this.topScoringIntent = topScoringIntent;
    } 
}

class TopScoringIntent {
    private String intent;

    public String getIntent() {
        return intent;
    }

    public void setIntent(String intent) {
        this.intent = intent;
    }
}

class Entity {
    private String entity;

    public String getEntity() {
        return entity;
    }

    public void setEntity(String entity) {
        this.entity = entity;
    }
}

With these, you can deserialise the response and get required values, eg: 有了这些,您可以反序列化响应并获得所需的值,例如:

    String responseBody = "{\n" + 
               "  \"query\": \"what is the weather like in texas\",\n" + 
               "  \"topScoringIntent\": {\n" + 
               "    \"intent\": \"GetWeather\",\n" + 
               "    \"score\": 0.697563648\n" + 
               "  },\n" + 
               "  \"entities\": [\n" + 
               "    {\n" + 
               "      \"entity\": \"texas\",\n" + 
               "      \"type\": \"Location\",\n" + 
               "      \"startIndex\": 28,\n" + 
               "      \"endIndex\": 32,\n" + 
               "      \"score\": 0.693443358\n" + 
               "    }\n" + 
               "  ]\n" + 
               "}";
   Gson gson = new GsonBuilder().create();
   Result jsonsluisresult = gson.fromJson(responseBody, Result.class);   
   System.out.println("Intent :" + jsonsluisresult.getTopScoringIntent().getIntent());
   System.out.println("Entity :" + jsonsluisresult.getEntities().get(0).getEntity());

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

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