android - W/System.err : org. json.JSONException

标签 android json

我如何从我的 Get 请求的响应中获取字段

这是我的回答

我想解析器请求获取并获取“referentiel”的值 这是我的代码

响应:

{
"status": "livre",
"referentiel": "000001498675",
"digitalid": "00004328",
"nom": "SAMI IDRISS",
"date": "10/04/2018 00:00:00",
"email": "",
"mobile": "123456789",
"Compte_principale": "0821006348788",
"Login": "Sami"
}

我想解析器请求获取并获取“referentiel”的值 这是我的代码

在此输入验证码:

  URL url = new URL(url1);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while(line != null){
            line = bufferedReader.readLine();
            data = data + line;
        }

        JSONArray JA = new JSONArray(data);
        for(int i =0 ;i <JA.length(); i++){
            JSONObject JO = (JSONObject) JA.get(i);
            singleParsed = (String) JO.get("referentiel");




            dataParsed = dataParsed + singleParsed +"\n" ;


        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }


       // }

请帮帮我

最佳答案

问题是您的data 响应不是对象数组而是单个对象。因此,您需要直接访问 JSONObject 的键,而不是使用 JSONArray

您当前的代码:

JSONArray JA = new JSONArray(data);
        for(int i =0 ;i <JA.length(); i++){
            JSONObject JO = (JSONObject) JA.get(i);
            singleParsed = (String) JO.get("referentiel");
            dataParsed = dataParsed + singleParsed +"\n" ;

更改为:

    JSONObject jsonObject = new JSONObject(data);
    singleParsed = (String) jsonObject.get("referentiel");
    dataParsed = dataParsed + singleParsed +"\n" ;

它会起作用。

关于android - W/System.err : org. json.JSONException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50401400/

相关文章:

android - 有没有比在每个命令后调用 glGetError 更好的调试 OpenGL 的方法?

javascript - 对 JSON 数组对象进行排序

javascript - 解析数组内的 JSON 数组并与 pdfmake 绑定(bind)

json - 如何上传多个json文件和表单数据到rest服务

java - 使用 Gson 进行 JSON 解析

android(singletouch)绘图应用程序撤消方法无法正常工作

Android:如何从应用程序将图像作为电子邮件附件发送?

java - 日期数组中最长的连续子序列

android - ADT/Eclipse - main.xml 布局编辑器中的图形/显示 Artifact 和移动文本行

java - Jackson 从子节点反序列化到字段?