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

时间:2019-04-01 07:57:53

标签: java json gson

我很难反序列化json对象和json对象的json数组。我想返回查询,意图和实体。我已经可以查询了。我想使用gson在“ topScoringIntent”下获得“ intent”,在“ entities”下获得“ entity”,并将它们全部归类。

                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:

{
  "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
    }
  ]
}

我想使用gson在“ topScoringIntent”下获得“ intent”,在“ entities”下获得“ entity”,并在一个类中将它们全部返回。

1 个答案:

答案 0 :(得分:0)

除了使用JsonObject之外,您还可以使用以下类型的POJO:

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;
    }
}

使用这些,您可以反序列化响应并获取所需的值,例如:

    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());