如何从json String获取值

时间:2017-03-05 18:43:19

标签: java android json key

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

我试图获得价值,但我没有得到它。

Json String就像:

{
    "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值并显示它。

2 个答案:

答案 0 :(得分:3)

所以你的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"
            }]
        }
    }
}

首先需要将“文档”设为JSONObject,然后将“businessCard”设为JSONObject,然后将“字段”设为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) { }
}

答案 1 :(得分:1)

试试这段代码,希望它会对你有帮助。

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