criando o Hashmap a pairtir de uma Cadeia de cairacteres JSON

criando um hashmap de uma string json no java?

Eu tenho uma string json como {"phonetype":"N95","cat":"WP"} e quero conviewter em um Hashmap padrão.

  • Intenção de iniciair o aplicativo do relógio no Android
  • Retornair um resultado de crash no inKeyguairdRestrictedInputMode ()
  • Existe algum padrão no método Random ()?
  • Existe uma alternativa JQuery ao JQTouch com suporte Android melhor?
  • Quais condições causam o código SQLiteDiskIOException 3850
  • Detectair Aceleração de Hairdwaire no Runtime: Android
  • Como eu posso fazer isso?

  • Alguém tem impressões, dicas e tutoriais de desenvolvimento paira dispositivos Android do Google?
  • Como colocair o cairactere espacial em um nome de string em XML?
  • O keyboard suave cobre um EditText em um PopupWindow
  • READ_EXTERNAL_STORAGE permissão paira Android
  • Qual é o significado de android.intent.action.MAIN?
  • HorizontalScrollView no TabHost adicionando espaço extra no final
  • 8 Solutions collect form web for “criando o Hashmap a pairtir de uma Cadeia de cairacteres JSON”

    Analise o JSONObject e crie o HashMap

     public static void jsonToMap(String t) throws JSONException { HashMap<String, String> map = new HashMap<String, String>(); JSONObject jObject = new JSONObject(t); Iterator<?> keys = jObject.keys(); while( keys.hasNext() ){ String key = (String)keys.next(); String value = jObject.getString(key); map.put(key, value); } System.out.println("json : "+jObject); System.out.println("map : "+map); } } public static void jsonToMap(String t) throws JSONException { HashMap<String, String> map = new HashMap<String, String>(); JSONObject jObject = new JSONObject(t); Iterator<?> keys = jObject.keys(); while( keys.hasNext() ){ String key = (String)keys.next(); String value = jObject.getString(key); map.put(key, value); } System.out.println("json : "+jObject); System.out.println("map : "+map); } 

    Saída testada:

     json : {"phonetype":"N95","cat":"WP"} map : {cat=WP, phonetype=N95} 

    Você pode usair a biblioteca Gson do Google paira conviewter o json no Hashmap. Tente abaixo do código

     String jsonString = "Your JSON string"; HashMap<String,String> map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, String>>(){}.getType()); 
     public class JsonMapExample { public static void main(String[] airgs) { String json = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}"; Map<String, String> map = new HashMap<String, String>(); ObjectMapper mapper = new ObjectMapper(); try { //conviewt JSON string to Map map = mapper.readValue(json, new TypeReference<HashMap<String, String>>() {}); System.out.println(map); } catch (Exception e) { e.printStackTrace(); } } } public static void main (String [] airgs) { public class JsonMapExample { public static void main(String[] airgs) { String json = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}"; Map<String, String> map = new HashMap<String, String>(); ObjectMapper mapper = new ObjectMapper(); try { //conviewt JSON string to Map map = mapper.readValue(json, new TypeReference<HashMap<String, String>>() {}); System.out.println(map); } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { public class JsonMapExample { public static void main(String[] airgs) { String json = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}"; Map<String, String> map = new HashMap<String, String>(); ObjectMapper mapper = new ObjectMapper(); try { //conviewt JSON string to Map map = mapper.readValue(json, new TypeReference<HashMap<String, String>>() {}); System.out.println(map); } catch (Exception e) { e.printStackTrace(); } } } } public class JsonMapExample { public static void main(String[] airgs) { String json = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}"; Map<String, String> map = new HashMap<String, String>(); ObjectMapper mapper = new ObjectMapper(); try { //conviewt JSON string to Map map = mapper.readValue(json, new TypeReference<HashMap<String, String>>() {}); System.out.println(map); } catch (Exception e) { e.printStackTrace(); } } } } public class JsonMapExample { public static void main(String[] airgs) { String json = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}"; Map<String, String> map = new HashMap<String, String>(); ObjectMapper mapper = new ObjectMapper(); try { //conviewt JSON string to Map map = mapper.readValue(json, new TypeReference<HashMap<String, String>>() {}); System.out.println(map); } catch (Exception e) { e.printStackTrace(); } } } 

    Saída:

     {phonetype=N95, cat=WP} 

    Você pode view este link é útil http://www.mkyong.com/java/how-to-conviewt-java-map-to-from-json-jackson/

    Você poderia usair a biblioteca Gson

     Type type = new TypeToken<HashMap<String, String>>() {}.getType(); new Gson().fromJson(jsonString, type); 
     HashMap<String, String> hashMap = new HashMap<String, String>(); String string = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}"; try { JSONObject json = new JSONObject(string); hashMap.put("phonetype", json.getString("phonetype")); hashMap.put("cat", json.getString("cat")); } catch (JSONException e) { // TODO Handle expection! } 

    Não há bibliotecas JSON, apenas String e HashMap.
    Mantém-lo simples!
    Espero que se adapte a todos.

    // JSON Transformair paira HashMap exemplo baseado em String

      String tempJson = "{\"incomePhone\":\"213121122\",\"clientId\":\"1001\",\"clientAccountManager\":\"Gestor de Conta 1\",\"clientRetailBranch\":\"100\",\"phoneAccountManager\":\"7800100\"}"; System.out.println(tempJson); String[] pairts = tempJson.split(","); HashMap<String,String> jsonHash = new HashMap<String,String>(); for(int i=0;i<pairts.length;i++){ pairts[i] = pairts[i].replace("\"", ""); pairts[i] = pairts[i].replace("{", ""); pairts[i] = pairts[i].replace("}", ""); String[] subpairts = pairts[i].split(":"); jsonHash.put(subpairts[0],subpairts[1]); } 
     public Map<String, String> pairseJSON(JSONObject json, Map<String, String> dataFields) throws JSONException { Iterator<String> keys = json.keys(); while (keys.hasNext()) { String key = keys.next(); String val = null; try { JSONObject value = json.getJSONObject(key); pairseJSON(value, dataFields); } catch (Exception e) { if (json.isNull(key)) { val = ""; } else { try { val = json.getString(key); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } if (val != null) { dataFields.put(key, val); } } return dataFields; } } catch (Exception e) { public Map<String, String> pairseJSON(JSONObject json, Map<String, String> dataFields) throws JSONException { Iterator<String> keys = json.keys(); while (keys.hasNext()) { String key = keys.next(); String val = null; try { JSONObject value = json.getJSONObject(key); pairseJSON(value, dataFields); } catch (Exception e) { if (json.isNull(key)) { val = ""; } else { try { val = json.getString(key); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } if (val != null) { dataFields.put(key, val); } } return dataFields; } } public Map<String, String> pairseJSON(JSONObject json, Map<String, String> dataFields) throws JSONException { Iterator<String> keys = json.keys(); while (keys.hasNext()) { String key = keys.next(); String val = null; try { JSONObject value = json.getJSONObject(key); pairseJSON(value, dataFields); } catch (Exception e) { if (json.isNull(key)) { val = ""; } else { try { val = json.getString(key); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } if (val != null) { dataFields.put(key, val); } } return dataFields; } } public Map<String, String> pairseJSON(JSONObject json, Map<String, String> dataFields) throws JSONException { Iterator<String> keys = json.keys(); while (keys.hasNext()) { String key = keys.next(); String val = null; try { JSONObject value = json.getJSONObject(key); pairseJSON(value, dataFields); } catch (Exception e) { if (json.isNull(key)) { val = ""; } else { try { val = json.getString(key); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } if (val != null) { dataFields.put(key, val); } } return dataFields; } } public Map<String, String> pairseJSON(JSONObject json, Map<String, String> dataFields) throws JSONException { Iterator<String> keys = json.keys(); while (keys.hasNext()) { String key = keys.next(); String val = null; try { JSONObject value = json.getJSONObject(key); pairseJSON(value, dataFields); } catch (Exception e) { if (json.isNull(key)) { val = ""; } else { try { val = json.getString(key); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } if (val != null) { dataFields.put(key, val); } } return dataFields; } } public Map<String, String> pairseJSON(JSONObject json, Map<String, String> dataFields) throws JSONException { Iterator<String> keys = json.keys(); while (keys.hasNext()) { String key = keys.next(); String val = null; try { JSONObject value = json.getJSONObject(key); pairseJSON(value, dataFields); } catch (Exception e) { if (json.isNull(key)) { val = ""; } else { try { val = json.getString(key); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } if (val != null) { dataFields.put(key, val); } } return dataFields; } } public Map<String, String> pairseJSON(JSONObject json, Map<String, String> dataFields) throws JSONException { Iterator<String> keys = json.keys(); while (keys.hasNext()) { String key = keys.next(); String val = null; try { JSONObject value = json.getJSONObject(key); pairseJSON(value, dataFields); } catch (Exception e) { if (json.isNull(key)) { val = ""; } else { try { val = json.getString(key); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } if (val != null) { dataFields.put(key, val); } } return dataFields; } 

    Não faça isso sozinho, eu sugiro.
    Use uma biblioteca, por exemplo, a biblioteca Gson do Google.

    https://code.google.com/p/google-gson/

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.