如何从json字符串中获取数值[英] How to get values from json String

问题描述

我有一个 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);
    }
}

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

此外,我可以像 Mobile 一样获取一个字段的两倍我可以获取两次,因此我想合并两个 Mobile 值并显示它.

推荐答案

所以你的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) { }
}

其他推荐答案

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

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();
}

本文地址:https://www.itbaoku.cn/post/978330.html