How to parse the Json response in android?

后端 未结 6 1761
一生所求
一生所求 2020-12-14 23:19

i m getting this response in a result of GET request to server

{\"LL\": { \"control\": \"dev/sys/getkey\", \"value\": \"4545453030304138303046392035343733373         


        
相关标签:
6条回答
  • 2020-12-14 23:35

    You can parse your response and get value try this:

    try {
      JSONObject jsonObject = new JSONObject(response);// Convert response string in to json object.
    
      JSONObject jsonLL = jsonObject.getJSONObject("LL");// Get LL json object from jsonObject.
    
      String strValue = jsonLL.getString("value");// Get value from jsonLL Object.
    
       } catch (Exception e) {
        e.printStackTrace();
       }
    
    0 讨论(0)
  • 2020-12-14 23:37

    you can parse current json String to get value from it as :

    // Convert String to json object
    JSONObject json = new JSONObject(responseText);
    
    // get LL json object
    JSONObject json_LL = json.getJSONObject("LL");
    
    // get value from LL Json Object
    String str_value=json_LL.getString("value"); //<< get value here
    
    0 讨论(0)
  • 2020-12-14 23:37

    Try this,

    JSONObject ResponseObject = new JSONObject(Response);
    String str = ResponseObject.getJSONObject("LL").getString(value);
    
    0 讨论(0)
  • 2020-12-14 23:38

    Try this:

    JSONObject json= json1.getJSONObject("LL");    
    
    String value= json.getString("value");
    
    0 讨论(0)
  • 2020-12-14 23:43

    Simple and Efficient solution : Use Googlle's Gson library

    • Put this in build.gradle file : implementation 'com.google.code.gson:gson:2.6.2'
    • Now convert the JSON String to a convenient datastrucutre like HashMap in 2 lines like this.

    Type type = new TypeToken<Map<String, String>>(){}.getType();
    Map<String, String> myMap = gson.fromJson(JsonString , type);
    

    or you can use this below class :

    To convert your JSON string to hashmap use this :

    HashMap<String, Object> hashMap = new HashMap<>(Utility.jsonToMap(response)) ;
    

    Use this class :) (handles even lists , nested lists and json)

    public class Utility {
    
        public static Map<String, Object> jsonToMap(Object json) throws JSONException {
    
            if(json instanceof JSONObject)
                return _jsonToMap_((JSONObject)json) ;
    
            else if (json instanceof String)
            {
                JSONObject jsonObject = new JSONObject((String)json) ;
                return _jsonToMap_(jsonObject) ;
            }
            return null ;
        }
    
    
       private static Map<String, Object> _jsonToMap_(JSONObject json) throws JSONException {
            Map<String, Object> retMap = new HashMap<String, Object>();
    
            if(json != JSONObject.NULL) {
                retMap = toMap(json);
            }
            return retMap;
        }
    
    
        private static Map<String, Object> toMap(JSONObject object) throws JSONException {
            Map<String, Object> map = new HashMap<String, Object>();
    
            Iterator<String> keysItr = object.keys();
            while(keysItr.hasNext()) {
                String key = keysItr.next();
                Object value = object.get(key);
    
                if(value instanceof JSONArray) {
                    value = toList((JSONArray) value);
                }
    
                else if(value instanceof JSONObject) {
                    value = toMap((JSONObject) value);
                }
                map.put(key, value);
            }
            return map;
        }
    
    
        public static List<Object> toList(JSONArray array) throws JSONException {
            List<Object> list = new ArrayList<Object>();
            for(int i = 0; i < array.length(); i++) {
                Object value = array.get(i);
                if(value instanceof JSONArray) {
                    value = toList((JSONArray) value);
                }
    
                else if(value instanceof JSONObject) {
                    value = toMap((JSONObject) value);
                }
                list.add(value);
            }
            return list;
        }
    }
    

    Thank me later :)

    0 讨论(0)
  • 2020-12-14 23:54

    try this

    JSONObject json = (JSONObject) JSONSerializer.toJSON(responseText);   
    String value = json.getJSONObject("LL").getString("value");
    
    0 讨论(0)
提交回复
热议问题