1

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.

3 Answers 3

3

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) { }
}
Sign up to request clarification or add additional context in comments.

5 Comments

and how to get the values from field array?
@Sid Updated with full details
@Sid That's because you're not using the code that I wrote for you. The issue is you're getting always index 0 instead of using i. array.getJSONObject(0) needs to be array.getJSONObject(i)
ohh yess.. silly mistakes,, and how about this 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

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

Comments

0

here is code

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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.