Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将Json解析为字符串android studio_Java_Android_Json - Fatal编程技术网

Java 将Json解析为字符串android studio

Java 将Json解析为字符串android studio,java,android,json,Java,Android,Json,我有一个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", "na

我有一个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\u module
adresse\u mac
mot\u de\u 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 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"}}
然后:


希望能有所帮助。

我建议使用谷歌的gson

查看教程

GSON使生活方式更易于操作

您应该尝试以下方法:

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
您还可以查看绩效奖金

教程:


或者由Google提供一种更优雅的api。

str
是一个
JSONObject
,而不是
JSONArray
请确保您在非GUI线程(新线程、异步任务等)上调用它,但我需要从URL解析没有GSONIt是最简单的方法,有一些替代方案,但这需要时间来编码
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