【问题标题】:cannot parse json to string format无法将 json 解析为字符串格式
【发布时间】: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"
    }]
}

【问题讨论】:

  • 发布日志猫.....
  • 你的问题不清楚。您是否尝试从 Data JSONArray 中获取 "user_name" 的值??
  • @Naz141 是的兄弟,就像你说的那样
  • @Champandorid System.out: org.json.JSONException: 索引 1 超出范围 [0..1)

标签: java android json parsing


【解决方案1】:

试试这个

  BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream()));
  StringBuilder total = new StringBuilder();
  String value;
  while ((value= r.readLine()) != null) {
      total.append(value).append('\n');
  }
  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;

【讨论】:

    【解决方案2】:

    试试这个。

                BufferedReader bf = new BufferedReader(new InputStreamReader(con.getInputStream()));
                response = convertStreamToString(in);
    JSONObject jObject = new JSONObject(response);
    JSONArray jsonMainArr = jObject.getJSONArray("Data"); 
    
      JSONObject jsonObj = jsonMainArr.getJSONObject(0);
    
    String username = jsonObj.getString("user_name");
    return username;
    
    
    
     private String convertStreamToString(InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
    
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append('\n');
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      相关资源
      最近更新 更多