How to access a specific field of a JSON object using JAVA

1

I'm using Java to get Twitter information and the ElasticSearch REST client .

As a test I made an info PUT in JSON and then used performRequest to get a specific element.

//create a json to PUT at the ES
    Map<String, String> params2 = Collections.emptyMap();
    String jsonString = "{" +
                "\"user\":\"Luisinho\"," +
                "\"postDate\":\"1988\"," +
                "\"message\":\"esta a começar a andar\"" +
            "}";
    HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
    Response response4 = restClient.performRequest("PUT", "/posts/doc/2", params, entity);


    //Search the document using Query Params

    Map<String, String> paramMap = new HashMap<String, String>();
    paramMap.put("q", "user:Luisinho");
    paramMap.put("pretty", "true");

    Response response5 = restClient.performRequest("GET", "/posts/_search",
                                                               paramMap);

    System.out.println(EntityUtils.toString(response5.getEntity()));
    System.out.println("Host -" + response5.getHost() );

And I got the desired element this way:

   {
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "posts",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.2876821,
        "_source" : {
          "user" : "Luisinho",
          "postDate" : "1988",
          "message" : "esta a começar a andar"
        }
      }
    ]
  }
}

Host -http://localhost:9200

1 - Is it possible to get this information already as a JSON object?

2 - As I do not know how to do the first one, I converted that information to a JSON object, but how do I get the value of the key "message" that interests me?

I tried something like:

String nova=EntityUtils.toString(response5.getEntity());
JSONObject jsonObj = new JSONObject(nova);
String message = jsonObj.getJSONObject("hits").getString("message");
        System.out.println("JSON  "+message);

But I can not get the value you want.

    
asked by anonymous 08.08.2017 / 21:20

1 answer

1

To make it simpler to get the "message" value. Create a pojo with all fields returned from json. After that, use the GSON library to convert your json to the created pojo. That way, you'll be able to get the value right!

Something similar to this:

Gson gson = new Gson();
String jsonInString = "   {
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "posts",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.2876821,
        "_source" : {
          "user" : "Luisinho",
          "postDate" : "1988",
          "message" : "esta a começar a andar"
        }
      }
    ]
  }
}";
PojoCriado pojoCriado= gson.fromJson(jsonInString, PojoCriado.class);
pojoCriado.getHits.getMessage();
    
08.08.2017 / 22:08