0
String jsonString  = "{"name":"kd","isMe":"yes","time":"10:12 AM"},{"name":"you","isMe":"no","time":"10:12 AM"}";

JSONObject jValueObject = new JSONObject(jsonString);

    Iterator<?> values = jValueObject.keys();
    while(values.hasNext()){
        String final_key = (String)values.next();
        String final_value = jValueObject.getString(final_key);
        if (final_value != "")
            map.put(final_key, final_value);

        if (!final_key.equals("")) {
            sbkeys.append(final_key).append(',');
        }
    }
    Log.e("Final_Map", String.valueOf(map));
    itemList = sbkeys.toString();

Output : {name=kd,isMe=yes,time=10:12 AM}

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Rohit B.
  • 5
  • 5
  • 1
    What is the error you are facing while running this code? Always be precise with your question. – karthik vishnu kumar Feb 27 '20 at 09:56
  • @karthikvishnukumar I am getting that String jsonString from previous JSON string that was i converted into hashmap what get value if mu hashmap that value is my string. This string i wanted into the hashmap format but i get in iteration that output – Rohit B. Feb 27 '20 at 11:34

4 Answers4

0

Try this code:

HashMap map = new HashMap<String, String>();
Iterator<?> values = jValueObject.keys();
while(values.hasNext()) {
    String final_key = (String)values.next();
    String final_value = jValueObject.optString(final_key, "");
    if (!final_value.equals(""))
        map.put(final_key, final_value);
}
for (k : map.keySet()) {
    Log.d("Final_Map", "map[" + key + "] : " + map.get(k));
}
easy_breezy
  • 1,073
  • 7
  • 18
0

this is a duplicate question, please check these answers Convert JSONObject to Map

use Jackson(http://jackson.codehaus.org/) from http://json.org/

HashMap<String,Object> result =
    new ObjectMapper().readValue(<JSON_OBJECT>, HashMap.class);

or

You can use Gson() (com.google.gson) library if you find any difficulty using Jackson.

HashMap<String, Object> yourHashMap = new Gson().fromJson(yourJsonObject.toString(), HashMap.class);
0

First off, if its a json array that you need in the string then wrap your string in square brackets. (I'd expect you've escaped the double quotes as well).

    String jsonString  = "[{\"name\":\"kd\",\"isMe\":\"yes\",\"time\":\"10:12 AM\"},{\"name\":\"you\",\"isMe\":\"no\",\"time\":\"10:12 AM\"}]";

Now its a JSONArray and not a JSONObject. So to get a list of Maps,

JSONArray jValueArray = new JSONArray(jsonString);
List<Object> listOfMaps = jValueArray.toList();
System.out.println(listOfMaps);

Prints:

[{isMe=yes, name=kd, time=10:12 AM}, {isMe=no, name=you, time=10:12 AM}]
Neeraj
  • 2,376
  • 2
  • 24
  • 41
  • I get String like"{"name":"kd","isMe":"yes","time":"10:12 AM"},{"name":"you","isMe":"no","time":"10:12 AM"}" . How can i add square brackets in that string. – Rohit B. Feb 27 '20 at 11:22
  • `String jsonString = "{\"name\":\"kd\",\"isMe\":\"yes\",\"time\":\"10:12 AM\"},{\"name\":\"you\",\"isMe\":\"no\",\"time\":\"10:12 AM\"}"; jsonString = "[" + jsonString + "]";` – Neeraj Feb 27 '20 at 11:27
  • What is the error ? and which library are you using for JSON ? – Neeraj Feb 27 '20 at 13:25
0
JSONObject jObject = new JSONObject(jsonString);
        Iterator<?> keys = jObject.keys();
        while (keys.hasNext()) {
            map = new HashMap<String, String>();
            sbkeys = new StringBuilder();

            String key = (String) keys.next();
            String value = jObject.getString(key);

            try{
                JSONObject jValueObject = new JSONObject(value);
                Iterator<?> values = jValueObject.keys();

                while (values.hasNext()) {
                    String final_key = (String) values.next();
                    String final_value = jValueObject.getString(final_key);

                    if (!final_value.equalsIgnoreCase("")) {
                        map.put(final_key, final_value);
                        sbkeys.append(final_key).append(',');
                    }
                }
            }catch (Exception e){
                e.fillInStackTrace();
            }
            try {
               //// Your Code..
            } catch (Exception e) {
                errorCode = "1";
            }
        }
Rohit B.
  • 5
  • 5