【问题标题】:How to get values from json String如何从json字符串中获取值
【发布时间】:2017-07-25 13:03:00
【问题描述】:

我有一个 json 字符串字符串,我想从中获取值。它是密钥对形式,所以我想要每个密钥的值。

我试图获取这些值,但我没有得到它。

Json 字符串是这样的:

{
    "document": {
        "xmlns:xsi": "http:\/\/www.w3.org\/2001\/XMLSchema-instance",
        "xsi:schemaLocation": "http:\/\/ocrsdk.com\/schema\/recognizeard-1.0.xsd http:\/\/ocrsdk.com\/schema\/recognized-1.0.xsd",
        "xmlns": "http:\/\/ocrsdk.com\/schema\/recognize-1.0.xsd",
        "businessCard": {
            "imageRotation": "noRotation",
            "field":

                [{
                "type": "Name",
                "value": "Rafcev B. Agnrwal"
            }, {
                "type": "Company",
                "value": "VJ>"
            }, {
                "type": "Text",
                "value": "Dr. Rafcev B. Agnrwal\nMOB 0324%\nun\n) AOM*. founts. sso\nVJ>\nT"
            }]
        }
    }
}

我想获取“姓名”、“地址”、“公司”等值。

我试着变成这样:

JSONArray array;
if(mIntent.getStringExtra("jsonString") != null) {
            Log.d("json", mIntent.getStringExtra("jsonString"));

        try {
            JSONObject object = new JSONObject(mIntent.getStringExtra("jsonString"));

            array = object.getJSONArray("field");

            for (int i = 0; i < array.length(); i++) {
                JSONObject subObject1 = array.getJSONObject(i);

                edt_FirstName.setText(object.getString("Name"));
            }

        } catch (JSONException e) {

}

谁能帮帮我?谢谢。。

编辑:

我试图检查这样的值:

for (int i = 0; i < array.length(); i++) {
    JSONObject subObject1 = array.getJSONObject(0);

    String type = subObject1.getString("type");
    String value = subObject1.getString("value");
    if (type.equals("Name")) {
        edt_FirstName.setText(value);
    }
    if(type.equals("Address"))
    {
        edt_address.setText(value);
    }
    if(type.equals("Company"))
    {
        edt_company.setText(value);
    }
    if(type.equals("Mobile"))
    {
        edt_Mobile.setText(value);
    }
}

我想从字段数组中获取所有值并设置为文本视图,但我只获取名称值而不获取其他值。

我也可以像 Mobile 一样获得两倍的字段,所以我想结合 Mobile 值并显示它。

【问题讨论】:

    标签: java android json key


    【解决方案1】:

    所以你的 json 对象如下:

    {
        "document": {
            "xmlns:xsi": "http:\/\/www.w3.org\/2001\/XMLSchema-instance",
            "xsi:schemaLocation": "http:\/\/ocrsdk.com\/schema\/recognizeard-1.0.xsd http:\/\/ocrsdk.com\/schema\/recognized-1.0.xsd",
            "xmlns": "http:\/\/ocrsdk.com\/schema\/recognize-1.0.xsd",
            "businessCard": {
                "imageRotation": "noRotation",
                "field":
    
                    [{
                    "type": "Name",
                    "value": "Rafcev B. Agnrwal"
                }, {
                    "type": "Company",
                    "value": "VJ>"
                }, {
                    "type": "Text",
                    "value": "Dr. Rafcev B. Agnrwal\nMOB 0324%\nun\n) AOM*. founts. sso\nVJ>\nT"
                }]
            }
        }
    }
    

    你首先需要得到“document”为JSONObject,然后得到“businessCard”为JSONObject,然后你可以得到“field”为JSONArray

    if(mIntent.getStringExtra("jsonString") != null) {
        Log.d("json", mIntent.getStringExtra("jsonString"));
    
        try {
            JSONObject object = new JSONObject(mIntent.getStringExtra("jsonString"));
    
            JSONArray array = object.getJSONObject("document").getJSONObject("businessCard").getJSONArray("field");
    
            for (int i = 0; i < array.length(); i++) {
                JSONObject subObject = array.getJSONObject(i);
    
                String type = subObject.getString("type");
                String value = subObject.getString("value");
                if (type.equals("Name")) {
                        String prevValue = edt_FirstName.getText();
                        edt_FirstName.setText((TextUtils.isEmpty(prevValue) ? "" : prevValue + ",") + value);
                }
            }
    
        } catch (JSONException e) { }
    }
    

    【讨论】:

    • 以及如何从字段数组中获取值?
    • @Sid 已更新完整详情
    • @Sid 那是因为你没有使用我为你写的代码。问题是你总是得到索引 0 而不是使用iarray.getJSONObject(0) 必须是 array.getJSONObject(i)
    • 哦,是的.. 愚蠢的错误,还有这个怎么样 我可以像 Mobile 一样获得两倍的字段,我可以获得两次,所以我想结合 Mobile 的值并显示它。?
    【解决方案2】:

    试试这个代码,希望对你有帮助。

    try
    {
        JSONObject jsonObject = new JSONObject();
        JSONObject documentObject = jsonObject.getJSONObject("document");
        JSONObject businessCardObject = documentObject.getJSONObject("businessCard");
    
        String imgRotation = businessCardObject.getString("imageRotation");
        JSONArray array = businessCardObject.getJSONArray("field");
    
        for (int i = 0; i < array.length() ; i++)
        {
            JSONObject arrObj = array.getJSONObject(i);
    
            String type = arrObj.getString("type");
            String value = arrObj.getString("value");
    
            Log.e(TAG, "type=="+type);
            Log.e(TAG, "value=="+value);
        }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    

    【讨论】:

      【解决方案3】:

      这里是代码

      private void convertJsonToDto(String jsonNewString) {
          JSONObject jsonObj = new JSONObject(jsonNewString);
          String RecordLocator = (String) jsonObj.get("obj");
          String FirstName = (String) jsonObj.getJSONObject("Customer").get("FirstName");
          JSONArray array = new JSONObject(jsonNewString).getJSONArray("Array");
          JSONObject jsonObject = array.getJSONObject(0);
          String str = jsonObject.getString("object");
      }
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 2021-07-13
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多