2012-01-04 9 views
18

Soy Begineer en Android.En mi proyecto, obtengo el siguiente json de la respuesta HTTP.Obtener valor de cadena del objeto Json Android

[{"Date":"2012-1-4T00:00:00","keywords":null,"NeededString":"this is the sample string I am needed for my project","others":"not needed"}] 

Quiero obtener el "NeededString" del json anterior.Cómo conseguirlo.

+0

https://androidbeasts.wordpress.com/2015/08/04/json-parsing-tutorial/ – Aakash

Respuesta

48

Esto podría ayudarlo.

JSONArray arr = new JSONArray(result); 
JSONObject jObj = arr.getJSONObject(0); 
String date = jObj.getString("NeededString"); 
+2

Gracias por la rápida respuesta. Entiendo. Muchas gracias. – Dray

+0

¿Qué es el código php del lado del servidor para enviar la respuesta? –

+0

aquí está mi formato json y estoy obteniendo json string como se indica a continuación. Pero quiero obtener un valor particular, ¿cómo obtenerlo? {"inicio de sesión": [{"estado": "error"}]} –

3

Incluya org.json.jsonobject en su proyecto.

entonces usted puede hacer esto:

JSONObject jresponse = new JSONObject(responseString); 
responseString = jresponse.getString("NeededString"); 

Suponiendo, responseString sostiene la respuesta que reciba.

Si lo que necesita saber cómo convertir la respuesta recibida en una cadena, aquí está cómo hacerlo:

ByteArrayOutputStream out = new ByteArrayOutputStream(); 
response.getEntity().writeTo(out); 
out.close(); 
String responseString = out.toString(); 
+0

gracias por la respuesta rápida. – Dray

8

sólo tiene que conseguir el JSONArray y repetir la JSONObject dentro de la matriz mediante un bucle aunque en su caso, es solo un objeto JSON pero puede tener más.

JSONArray mArray; 
     try { 
      mArray = new JSONArray(responseString); 
      for (int i = 0; i < mArray.length(); i++) { 
        JSONObject mJsonObject = mArray.getJSONObject(i); 
        Log.d("OutPut", mJsonObject.getString("NeededString")); 
       } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
+0

Gracias por la respuesta rápida. – Dray

+0

¿Qué es el código php del lado del servidor para enviar la respuesta? (Con esa clave "NeededString") –

1

Si puede utilizar JSONObject biblioteca, sólo podría

JSONArray ja = new JSONArray("[{\"Date\":\"2012-1-4T00:00:00\",\"keywords\":null,\"NeededString\":\"this is the sample string I am needed for my project\",\"others\":\"not needed\"}]"); 
    String result = ja.getJSONObject(0).getString("NeededString"); 
+0

gracias por la respuesta rápida ... – Dray

0

creo que es atento a que

   JSONArray jre = objJson.getJSONArray("Result"); 

       for (int j = 0; j < your array NAme.length(); j++) { 
        JSONObject jobject = your array NAme.getJSONObject(j); 

        String date = jobject.getString("Date"); 
        String keywords=jobject.getString("keywords"); 
        String needed=jobject.getString("NeededString"); 

       } 
0

Aquí está la solución que utilicé para mí se trabaja para ir a buscar JSON desde la cadena

protected String getJSONFromString(String stringJSONArray) throws JSONException { 
     return new StringBuffer(
       new JSONArray(stringJSONArray).getJSONObject(0).getString("cartype")) 
        .append(" ") 
        .append(
       new JSONArray(employeeID).getJSONObject(0).getString("model")) 
       .toString(); 
    } 
2

Puede utilizar getString

String name = jsonObject.getString("name"); 
// it will throws exception if the key you specify doesn't exist 

o optString

String name = jsonObject.optString("name"); 
// it will returns the empty string ("") if the key you specify doesn't exist 
Cuestiones relacionadas