0
String sUrl = "my_url.com";
URL url = new URL(sUrl);
URLConnection request = url.openConnection();
request.connect();

JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
JsonObject rootobj = root.getAsJsonObject();
System.out.println("JSON$$$$$$$$$$$ " + rootobj.get("data"));

My output is like this:

{
  "data" : [ {
    "i" : "#######",
    "d" : "#######",
    "values" : {
      "Json" : "[ {    "a": "******",     "b": "*******",     "c": "*****"}]"

But I want my output to be like this:

[ { "a": "******", "b": "*******", "c": "*****"}]

I want to remove the above stuff. How do I call JSON from data?

ADyson
  • 57,178
  • 14
  • 51
  • 63
iamsj
  • 84
  • 13
  • Just don't use "get("data")" as it takes the whole part with key daya, go deep and get value, then Json – azro Nov 08 '18 at 15:02
  • looks like you've got some badly-formed JSON - a JSON object with a string containing another stringified JSON array within it. Anyway now the main bit is deserialised, you can navigate down it like any other object - forget about that being JSON any more, it's no longer text, it's an object of type `JsonObject`...it will have properties and methods you can use to manipulate it. Find the "data" property, then within that object find the "values" property, and then within that find the "Json" property, and then deserialise the string within that to get the inner JSON you're interested in. – ADyson Nov 08 '18 at 15:02

2 Answers2

0

System.out.println("JSON$$$$$$$$$$$ " + rootobj.get("data")[2].json);

?

GaddMaster
  • 331
  • 3
  • 14
0

Exactly as @ADyson said, you must treat the Json Object, since it is a tree structure. So you have to "navigate" through the Json objects.

In this case it would be:

rootobj.get("data").getAsJsonArray().get(0).getAsJsonObject().get("values").getAsJsonObject().get("Json").getAsJsonArray()

If you have written well the Json ... and what you want to receive ...

A greeting

  • P.D.: The `[` are ArraysJson, and you have to take them according to the position, and the `{"key" : "value"` are json Objects, that you have to take according to their key-value pair – ElPendenciero Nov 08 '18 at 15:27
  • Thnq so much :) – iamsj Nov 09 '18 at 06:53
  • I have other question here.I dont know what is inside "value" to be clear I dont know what is in the place of "Json" since that value("Json ") is given by end user but that is definetly inside "value".How do I fetch that? – iamsj Nov 09 '18 at 08:16
  • Json objects are composed of a key and a value, the value can be perfectly another Json object (with their respective, key and value) so it can be a tree structure. In your case, the string "values" is a key, which has as value another Json object, which has as key "Json" and as value a JsonObject Array (with three Json objects: {"a": "**** ** "," b ":" ******* "," c ":" ***** "}) If I understand your question well, the user will look for the "Json" key that is inside the Json object with the "values" key. True? – ElPendenciero Nov 09 '18 at 09:28
  • So what you have to do is that the search already contains the structure before the user response, that is: `String userResponse = "Json";` `JsonArray myArray = rootobj.get ("data"). GetAsJsonArray (). Get (0) .getAsJsonObject (). Get ("values"). GetAsJsonObject (). Get (userResponse) .getAsJsonArray ()` And already use the array as you want (Keep in mind that this is still a Json Array with three Json Objects inside it and you should treat it as such). I do not know if this is what you asked, but I hope it helps you. – ElPendenciero Nov 09 '18 at 09:29