How to get values from json String

2019-02-20 09:37发布

问题:

I have a json string string from which I want to get the values. It is in key pair form so I want the values for each key.

I tried to get the values but I am not getting it.

Json String is like :

{
    "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"
            }]
        }
    }
}

I want to get values of "Name", "Address", "Company" etc.

I tried to get like this:

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) {

}

Can anyone help me out please? Thank you..

EDIT:

I tried to check values like this:

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

I want to get all the values from field array and set to the text view, but I am getting only the Name value and not the others.

Also I could get one field as twice like Mobile I can get twice, so I want to combine both the Mobile values and show it.

回答1:

So your json object is as follow:

{
    "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"
            }]
        }
    }
}

You first need to get "document" as JSONObject and then get "businessCard" as JSONObject and then you can get "field" as 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) { }
}


回答2:

Try this code, Hope it will help you.

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