如何在android中解析Json响应?

如何在android中解析Json响应?

问题描述:

I M获得在GET请求的结果,这响应于服务器如何在android中解析Json响应?

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

我只是想从上面的JSON响应中提取的"value"值。

我米使用此代码来获得这种反应

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(); 

     } 

    }); 

我的问题是,我该如何解析这个并获得唯一"value"变量的值。 感谢

可以解析当前的JSON字符串,从它得到value

// 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:平均IM没有得到ü。是的,你从服务器得到json字符串 – 2013-02-12 05:32:44

试试这个,

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

感谢您的帮助..我想问一个这更多。这是我得到的Json回应?并解析 – 2013-02-12 04:46:05

+0

与@ρяσѕρєяK的答案有什么不同? – usman 2014-06-02 07:25:43

试试这个:

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

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

即时通讯不能找到任何Json数组在当前JSON字符串提供的问题? – 2013-02-11 13:14:05

+0

LL不是jsonArray,它是jsonObject – mihail 2013-02-11 13:32:14

+0

与@ρяσѕρєяK的答案有什么不同? – usman 2014-06-02 07:24:44

试试这个

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

您可以分析您回应并获得价值试试这个:

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(); 
    }