Get specific value from a JSON JAVA URL

Asked

Viewed 422 times

0

Good morning Friends.

I’m having a hard time handling information collected in a third-party online JSON url, my APP needs information from various websites that send back different ways in JSON queries, below I’ll illustrate the modes that return and which I can handle.

Example of return 1.

{"alface": "12.23", "pepino": "12.43","batata": "12.49"}

I can capture and treat the way I want.

Example of return 2.

{"quitanda 1": {"alface": "12.23", "pepino": "12.43"}}

I can capture and treat the way I want.

Example of return 3. ( I can’t get any value )

[{"id":"alface","name": "ALFACE","symbol": "AF",},{"id": "pepino","name": "PEPINO","symbol": "PE",},{"id": "batata","name": "BATATA","symbol": "BA",}]

In this third-party JSON URL return type I can’t do anything, when the bracket enters [] I can’t pick up values or navigate inside the return in any way, someone could send me an example code capturing and printing on screen, any value of the third return mode, can be either in JSON or in GSON.

I’m doing my APP in JAVA in ECLIPSE, it’s not an app for a studio only put the same subjects in return to be clearer.

1 answer

0


You can turn the return of json into a map, then search by key and value

for example:

HashMap<String,Object> result =
    new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);
  • Thanks, I could apply your answer using one of the fields of the example I passed, because I have no experience in using map, an example would be to take any value inside the keys "potato".

  • In creating the map, roughly speaking, from your json examples, everything on the left side of the ":" will be keys, on the right side of the ":" will be values, in the potato example, would be result.get("potato"), this access is possible because in the declaration I have typed the key as String and the return at the highest level of generalization Object.

  • Okay, in the command that passed me above, I put my json object that I captured and put where you highlighted "JSON_SOURCE", after that I can put a variable to receive the "result.get("potato)", it would be more or less this?

  • Thanks for the support, it worked, but I decided to use another method I found on the internet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.