【发布时间】:2017-04-20 09:11:48
【问题描述】:
谁能告诉我为什么在执行 json 解析后没有得到数据的字符串格式值 ps:连接部分一切正常,现在我只想在其中添加json解析方法。
try {
strUrl = "http://10.0.2.2/android/login.php?username=" + u + "&password=" + p + "";
URL url = new URL(strUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.connect();
BufferedReader bf = new BufferedReader(new InputStreamReader(con.getInputStream()));
String value = bf.readLine();
String json = value;
JSONObject parentObject = new JSONObject(json);
JSONArray parentArray = parentObject.getJSONArray("Data");
JSONObject finalObject = parentArray.getJSONObject(0);
String username = finalObject.getString("user_name");
String result = username;
return result;
这是我的 api 结果:
{
"Status": "true",
"Message": "login successfully",
"Data": [{
"user_name": "JS"
}]
}
【问题讨论】:
-
发布日志猫.....
-
你的问题不清楚。您是否尝试从
DataJSONArray 中获取"user_name"的值?? -
@Naz141 是的兄弟,就像你说的那样
-
@Champandorid System.out: org.json.JSONException: 索引 1 超出范围 [0..1)