将Json解析为String Android Studio
我有这个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"
}
}
而且我想解析,并获得字符串id_module
,adresse_mac
,mot_de_passe
和name
每个事物的1和2。
所以我做了这个,但是没有用:
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();
}
回答:
您应该尝试这样:
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
以上是 将Json解析为String Android Studio 的全部内容, 来源链接: utcz.com/qa/401750.html