2013-02-11 18 views
8

tôi đang nhận được phản ứng này trong kết quả của yêu cầu GET đến máy chủLàm thế nào để phân tích phản ứng Json trong Android?

{"LL": { "control": "dev/sys/getkey", "value": "4545453030304138303046392035343733373432363020323031332D30322D31312031383A30313A3135", "Code": "200"}} 

tôi chỉ muốn trích xuất các giá trị của "value" từ phản ứng json trên.

Tôi đang sử dụng mã này để có được câu trả lời này

findViewById(R.id.button1).setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      HttpResponse response = null; 
      try { 
       HttpClient client = new DefaultHttpClient(); 
       HttpGet request = new HttpGet(); 
       request.setURI(new URI(
         "http://192.168.14.247/jdev/sys/getkey")); 
       response = client.execute(request); 
      } catch (URISyntaxException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      String responseText = null; 
      try { 
       responseText = EntityUtils.toString(response.getEntity()); 
      } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Log.i("Parse Exception", e + ""); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Log.i("IO Exception 2", e + ""); 

      } 

      Log.i("responseText", responseText); 

      Toast.makeText(MainActivity.this, responseText + "", 
        Toast.LENGTH_SHORT).show(); 

     } 

    }); 

câu hỏi của tôi là làm thế nào tôi có thể phân tích này và nhận được giá trị của chỉ "value" thẻ. nhờ

Trả lời

18

bạn có thể phân tích hiện json String để có được value từ nó như:

// Convert String to json object 
JSONObject json = new JSONObject(responseText); 

// get LL json object 
JSONObject json_LL = json.getJSONObject("LL"); 

// get value from LL Json Object 
String str_value=json_LL.getString("value"); //<< get value here 
+0

@QadirHussain: trung bình im không nhận được u. có u đang nhận chuỗi json từ máy chủ –

0

Hãy thử điều này,

JSONObject ResponseObject = new JSONObject(Response); 
String str = ResponseObject.getJSONObject("LL").getString(value); 
+0

Cảm ơn bạn đã trợ giúp .. Tôi muốn hỏi thêm một câu hỏi nữa. này là Json Response i m nhận được? và phân tích cú pháp –

+0

khác với câu trả lời @ ρяσѕρєяK như thế nào? – usman

2

Hãy thử điều này:

JSONObject json= json1.getJSONObject("LL");  

String value= json.getString("value"); 
+0

im không thể tìm thấy bất kỳ Json Array trong chuỗi json hiện tại như được cung cấp với câu hỏi? –

+0

LL không phải là jsonArray, nó là jsonObject – mihail

+0

khác với câu trả lời @ ρяσѕρєяK như thế nào? – usman

2

thử

này
JSONObject json = (JSONObject) JSONSerializer.toJSON(responseText); 
String value = json.getJSONObject("LL").getString("value"); 
0

Bạn có thể phân tích phản ứng của bạn và nhận được giá trị thử điều này:

try { 
    JSONObject jsonObject = new JSONObject(response);// Convert response string in to json object. 

    JSONObject jsonLL = jsonObject.getJSONObject("LL");// Get LL json object from jsonObject. 

    String strValue = jsonLL.getString("value");// Get value from jsonLL Object. 

    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
Các vấn đề liên quan