2012-01-04 45 views
18

Tôi là người bắt đầu trong Android.Trong dự án của tôi, tôi nhận được json theo sau từ Phản hồi HTTP.Nhận giá trị chuỗi từ đối tượng 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"}] 

Tôi muốn nhận được "NeededString" từ json ở trên. Làm thế nào để có được nó.

+0

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

Trả lời

48

Điều này có thể giúp bạn.

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

Cảm ơn bạn đã nhanh chóng Đáp lại. Tôi đạt được rồi. Cảm ơn rất nhiều. – Dray

+0

Mã php ở phía máy chủ để gửi phản hồi là gì? –

+0

đây là định dạng json của tôi và tôi nhận được chuỗi json như được đưa ra dưới đây. Nhưng tôi muốn nhận được giá trị cụ thể, làm thế nào để có được nó? {"login": [{"status": "error"}]} –

3

Bao gồm org.json.jsonobject trong dự án của bạn.

Sau đó bạn có thể làm điều này:

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

Giả sử, responseString nắm giữ câu trả lời bạn nhận được.

Nếu bạn cần phải biết làm thế nào để chuyển đổi câu trả lời nhận được vào một String, dưới đây là cách để làm điều đó:

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

cảm ơn bạn đã trả lời nhanh. – Dray

8

Bạn chỉ cần để có được những JSONArray và lặp các JSONObject bên trong mảng sử dụng một vòng lặp dù trong trường hợp của bạn chỉ có một JSONObject nhưng bạn có thể có nhiều hơn.

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

Cảm ơn bạn đã trả lời nhanh. – Dray

+0

Mã php ở phía máy chủ để gửi phản hồi là gì? (Với phím "NeededString") –

1

Nếu bạn có thể sử dụng JSONObject thư viện, bạn có thể chỉ

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

cảm ơn bạn đã trả lời nhanh ... – Dray

0

tôi nghĩ rằng hữu ích của nó đối với bạn

   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

Dưới đây là giải pháp tôi sử dụng cho tôi là công trình cho việc lấy JSON từ chuỗi

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

Bạn có thể sử dụng getString

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

hoặc optString

String name = jsonObject.optString("name"); 
// it will returns the empty string ("") if the key you specify doesn't exist 
Các vấn đề liên quan