How do I extract value from Json

前端 未结 8 1096
野的像风
野的像风 2020-11-30 05:23

I am getting a response String from server like below

{
  \"name\": \"Json\",
  \"detail\": {
    \"first_name\": \"Json\",
    \"last_name\": \"Scott\",
            


        
相关标签:
8条回答
  • 2020-11-30 05:35

    Use a JSON parser. There are plenty of JSON parsers written in Java.

    http://www.json.org/

    Look under the Java section and find one you like.

    0 讨论(0)
  • 2020-11-30 05:35

    we can use the below to get key as string from JSON OBJECT

    JsonObject json = new JsonObject(); 
     json.get("key").getAsString();
    

    this gives the string without double quotes " " in the string

    0 讨论(0)
  • 2020-11-30 05:36

    If you don't mind adding a dependency, you can use JsonPath.

    import com.jayway.jsonpath.JsonPath;
    
    String firstName = JsonPath.read(rawJsonString, "$.detail.first_name");
    

    "$" specifies the root of the raw json string and then you just specify the path to the field you want. This will always return a string. You'll have to do any casting yourself.

    Be aware that it'll throw a PathNotFoundException at runtime if the path you specify doesn't exist.

    0 讨论(0)
  • 2020-11-30 05:39

    see this code what i am used in my application

    String data="{'foo':'bar','coolness':2.0, 'altitude':39000, 'pilot':{'firstName':'Buzz','lastName':'Aldrin'}, 'mission':'apollo 11'}";
    

    I retrieved like this

    JSONObject json = (JSONObject) JSONSerializer.toJSON(data);        
        double coolness = json.getDouble( "coolness" );
        int altitude = json.getInt( "altitude" );
        JSONObject pilot = json.getJSONObject("pilot");
        String firstName = pilot.getString("firstName");
        String lastName = pilot.getString("lastName");
    
        System.out.println( "Coolness: " + coolness );
        System.out.println( "Altitude: " + altitude );
        System.out.println( "Pilot: " + lastName );
    
    0 讨论(0)
  • 2020-11-30 05:39

    Pasting my code here, this should help. It shows the package which can be used.

    import org.json.JSONException;
    import org.json.JSONObject;
    
    public class extractingJSON {
    
        public static void main(String[] args) throws JSONException {
            // TODO Auto-generated method stub
    
            String jsonStr = "{\"name\":\"SK\",\"arr\":{\"a\":\"1\",\"b\":\"2\"}}";
            JSONObject jsonObj = new JSONObject(jsonStr);
            String name = jsonObj.getString("name");
            System.out.println(name);
    
            String first = jsonObj.getJSONObject("arr").getString("a");
            System.out.println(first);
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-30 05:39
    String jsonErrorString=((HttpClientErrorException)exception).getResponseBodyAsString();
    JSONObject jsonObj=null;
    String errorDetails=null;
    String status=null;
    try {
            jsonObj = new JSONObject(jsonErrorString);
            int index =jsonObj.getString("detail").indexOf(":");
                    errorDetails=jsonObj.getString("detail").substring(index);
                    status=jsonObj.getString("status");
    
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                item.put("status", status);
                item.put("errordetailMsg", errorDetails);
    
    0 讨论(0)
提交回复
热议问题