Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find specified name and its value in JSON-string from Java? [closed]

Let's assume we have the next JSON string:

{      "name" : "John",    "age" : "20",    "address" : "some address",    "someobject" : {        "field" : "value"        } } 

What is the easiest (but still correct, i.e. regular expressions are not acceptable) way to find field age and its value (or determine that there's no field with given name)?

p.s. any open-source libs are ok.

p.s.2: please, don't post links to the libraries - it's not a useful answer. 'Show me the code'(c).

like image 823
Roman Avatar asked May 27 '11 13:05

Roman


People also ask

How do I extract value from JSON?

To extract the scalar value from the JSON string, use the json_extract_scalar function. It is similar to json_extract , but returns only scalar values (Boolean, number, or string).

How do you escape the special characters in a JSON String in Java?

You can escape String in Java by putting a backslash in double quotes e.g. " can be escaped as \" if it occurs inside String itself. This is ok for a small JSON String but manually replacing each double quote with an escape character for even a medium-size JSON is time taking, boring, and error-prone.

How do you check the type of a value from a JSON object?

You can get the object from the JSON with the help of JSONObject. get() method and then using the instanceof operator to check for the type of Object. Note that it may be Integer or Long ; Float or Double .

How do you check if a key exists in a JSON String in Java?

Use below code to find key is exist or not in JsonObject . has("key") method is used to find keys in JsonObject . If you are using optString("key") method to get String value then don't worry about keys are existing or not in the JsonObject . Note that you can check only root keys with has(). Get values with get().


2 Answers

Use a JSON library to parse the string and retrieve the value.

The following very basic example uses the built-in JSON parser from Android.

String jsonString = "{ \"name\" : \"John\", \"age\" : \"20\", \"address\" : \"some address\" }"; JSONObject jsonObject = new JSONObject(jsonString); int age = jsonObject.getInt("age"); 

More advanced JSON libraries, such as jackson, google-gson, json-io or genson, allow you to convert JSON objects to Java objects directly.

like image 51
devconsole Avatar answered Sep 17 '22 05:09

devconsole


Gson allows for one of the simplest possible solutions. Compared to similar APIs like Jackson or svenson, Gson by default doesn't even need the unused JSON elements to have bindings available in the Java structure. Specific to the question asked, here's a working solution.

import com.google.gson.Gson;  public class Foo {   static String jsonInput =      "{" +        "\"name\":\"John\"," +        "\"age\":\"20\"," +        "\"address\":\"some address\"," +        "\"someobject\":" +       "{" +          "\"field\":\"value\"" +        "}" +      "}";    String age;    public static void main(String[] args) throws Exception   {     Gson gson = new Gson();     Foo thing = gson.fromJson(jsonInput, Foo.class);     if (thing.age != null)     {       System.out.println("age is " + thing.age);     }     else     {       System.out.println("age element not present or value is null");     }   } } 
like image 32
Programmer Bruce Avatar answered Sep 20 '22 05:09

Programmer Bruce