Parse Json en el estudio android de String
Tengo este objeto JSON:
{ "1":{ "id_module":"f83d6101cc", "adresse_mac":"00:6A:8E:16:C6:26", "mot_de_passe":"mp0001","name":"a" }, "2":{ "id_module":"64eae5403b", "adresse_mac":"00:6A:8E:16:C6:26", "mot_de_passe":"mp0002", "name":"a" } }
Y me gustaría analizar y llegar a la cadena id_module
, adresse_mac
, mot_de_passe
y el name
de cada cosa el 1 y el 2.
- ¿Cómo utilizar Gson para serializar objetos en android?
- Conexión de Android a localhost
- ¿Cómo calcular el precio en la cantidad de datos JSON y Post datos?
- Enviar JSON al servidor vía HTTP poner solicitud en android
- Gson Array de deserialización de matrices
Así que hice esto pero no funciona:
TextView txt1=(TextView) findViewById(R.id.textView); String ajout1 = "http://"; JSONObject json = null; String str = "1"; HttpResponse response; HttpClient myClient = new DefaultHttpClient(); HttpPost myConnection = new HttpPost(ajout1); try { response = myClient.execute(myConnection); str = EntityUtils.toString(response.getEntity(), "UTF-8"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { JSONObject jsonObject = new JSONObject(str); String MAC = jsonObject.getString("id_module"); txt1.setText(MAC); } catch (JSONException e) { e.printStackTrace(); }
- Forzar a GSON a usar Constructor que contenga Setters o Setters en general, en lugar de Fields
- HttpClient.execute lanzando OutOfMemoryError
- Hacer caso de JsonGetter insensible
- Múltiples elementos seleccionados RecyclerView in Activity.java
- ¿Cómo analizar después de JSON usando GSON en android?
- JSON `date (...)` a `java.Util.Date` usando` org.json`
- Best REST Client Framework / Utilidad en Android
- Deserialización GSON con tipos genéricos y nombres genéricos de campos
Usted debe intentar esto:
String str = "your json string"; JSONObject json = new JSONObject(str); String module = json.getJSONObject("1").getString("id_module"); String address = json.getJSONObject("1").getString("adresse_mac"); String module2 = json.getJSONObject("2").getString("id_module"); //from 2nd object
Recomiendo el uso de gson de Google.
Echa un vistazo a este tutorial
GSON ¡Hace la manera viva más handable!
También puede revisar a Jackson por un bono de rendimiento.
Tutorial: JackSon en 5 minutos
O Gson de Google para un api un poco más elegante.
Prueba esto:
String json = {"1":{"id_module":"f83d6101cc","adresse_mac":"00:6A:8E:16:C6:26","mot_de_passe":"mp0001","name":"a"},"2":{"id_module":"64eae5403b","adresse_mac":"00:6A:8E:16:C6:26","mot_de_passe":"mp0002","name":"a"}}
Entonces:
JSONObject obj = new JSONObject(json);
Espero que ayude.
- Context.registerReceiver devuelve una intención nula al intentar comprobar si la fuente de alimentación está conectada
- Obtener el valor edittext del fragmento