0
org.json.JSONException: Value NeededTypes of type java.lang.String cannot be converted to JSONArray

i got this error when i am trying get JSONArray neededTypesArray if u have any ideas how to fix it, i will be very gratefull!

here is my jsonString:

[  {"NeededTypes":
            [{"DonationTypeId":1},       
             {"DonationTypeId":2}],
        "FirstName":"Катерина",
        "MiddleName":null,
        "LastName":"Дяченко",
        "DateOfBirth":"2002-05-07T00:00:00",
        "DateOfDead":null,
        "DonorCenterId":13,
        "PhotoImage":"http://donorua.blob.core.windows.net/public-images/be359926-22d3-49bd-9ecd-b2c5a41dc988.jpg",
        "ContactPerson":"Наталія (мати)",
        "IsDead":false,
        "IsApproved":true,
        "IsUrgent":true,
        "DateAdded":"2015-03-29T10:51:15.58",
        "BloodGroupId":4,
        "LastEditDate":null,
        "Disease":"description",
        "Id":25,
        "Email":null,
        "Phone":"096 714 99 25",
        "Longitude":0.0,
        "Latitude":0.0},
    ...]

here is my class getData:

private void getRecipientsDataFromJson(String dataJson)
            throws JSONException {

        final String RECIP_ID = "Id";
        final String RECIP_CENTER = "DonorCenterId";
        final String RECIP_LASTNAME = "LastName";
        final String RECIP_FIRSTNAME = "FirstName";
        final String RECIP_DATE_OF_BIRTH = "DateOfBirth";
        final String RECIP_BLOOD_GROUP = "BloodGroupId";
        final String RECIP_NEEDED_TYPES = "NeededTypes";
        final String RECIP_DON_TYPE_ID = "DonationTypeId";
        final String RECIP_DISEASE = "Disease";
        final String RECIP_PHOTO_IMAGE = "PhotoImage";
        final String RECIP_CONTACT_PERSON = "ContactPerson";
        final String RECIP_CONTACT_PHONE = "Phone";
        final String RECIP_DESCRIPTION = "Description";

        try {
            JSONArray recipientsArray = new JSONArray(dataJson);
            Vector<ContentValues> cVVector = new Vector<ContentValues>(recipientsArray.length());
            for (int i = 0; i < recipientsArray.length(); i++) {
                int id;
                int center;
                String lastName;
                String firstName;
                String birthDay;
                int bloodGroupId;
                String donationType = "";
                String disease;
                String photoImage;
                String contactPerson;
                String contactPhone;
                String description;

                JSONArray neededTypesArray = new JSONArray(RECIP_NEEDED_TYPES);
                for (int j = 0; j < 3; j++){
                    JSONObject currentDonationType = neededTypesArray.getJSONObject(j);
                    donationType = donationType.concat(currentDonationType.getString(RECIP_DON_TYPE_ID));
                }

                JSONObject currentRecipient = recipientsArray.getJSONObject(i);

                id = currentRecipient.getInt(RECIP_ID);
                center = currentRecipient.getInt(RECIP_CENTER);
                lastName = currentRecipient.getString(RECIP_LASTNAME);
                firstName = currentRecipient.getString(RECIP_FIRSTNAME);
                birthDay = currentRecipient.getString(RECIP_DATE_OF_BIRTH);
                bloodGroupId = currentRecipient.getInt(RECIP_BLOOD_GROUP);
                disease = currentRecipient.getString(RECIP_DISEASE);
                photoImage = currentRecipient.getString(RECIP_PHOTO_IMAGE);
                contactPerson = currentRecipient.getString(RECIP_CONTACT_PERSON);
                contactPhone = currentRecipient.getString(RECIP_CONTACT_PHONE);
                description = currentRecipient.getString(RECIP_DESCRIPTION);

                ContentValues recipientValues = new ContentValues();

                recipientValues.put(RecipientsEntry.COLUMN_RECIPIENT_ID, id);
                recipientValues.put(RecipientsEntry.COLUMN_CENTER_KEY, center);
                recipientValues.put(RecipientsEntry.COLUMN_LAST_NAME, lastName);
                recipientValues.put(RecipientsEntry.COLUMN_FIRST_NAME, firstName);
                recipientValues.put(RecipientsEntry.COLUMN_BIRTH_DAY, birthDay);
                recipientValues.put(RecipientsEntry.COLUMN_BLOOD_GROUP_ID, bloodGroupId);
                recipientValues.put(RecipientsEntry.COLUMN_DONATION_TYPE , donationType );
                recipientValues.put(RecipientsEntry.COLUMN_DISEASE , disease);
                recipientValues.put(RecipientsEntry.COLUMN_PHOTO_IMAGE , photoImage);
                recipientValues.put(RecipientsEntry.COLUMN_CONTACT_PERSON , contactPerson);
                recipientValues.put(RecipientsEntry.COLUMN_CONTACT_PHONE , contactPhone);
                recipientValues.put(RecipientsEntry.COLUMN_DESC , description);

                cVVector.add(recipientValues);
            }
            int inserted = 0;
            if (cVVector.size() > 0){
                ContentValues[] cvArray = new ContentValues[cVVector.size()];
                cVVector.toArray(cvArray);
                inserted = mContext.getContentResolver().bulkInsert(RecipientsEntry.CONTENT_URI, cvArray);
            }
            Log.d(LOG_TAG, "getRecipientsDataFromJson Complete. " + inserted + " Inserted");
        } catch (JSONException e){
            e.printStackTrace();
        }
    }
4
  • What line is causing the exception? Commented Apr 10, 2015 at 19:56
  • @Eric, JSONArray neededTypesArray = new JSONArray(RECIP_NEEDED_TYPES); Commented Apr 10, 2015 at 19:57
  • 1
    @Andriy string "NeededTypes" is not in JsonArray format Commented Apr 10, 2015 at 20:00
  • @shijil, i guess :) but why? it has two child... Commented Apr 10, 2015 at 20:04

3 Answers 3

3

make this change your code

JSONObject currentRecipient = recipientsArray.getJSONObject(i);
JSONArray neededTypesArray = currentRecipient.getJSONArray(RECIP_NEEDED_TYPES);
    for (int j = 0; j < neededTypesArray.length(); j++){
        JSONObject currentDonationType = neededTypesArray.getJSONObject(j);
        onationType = donationType.concat(currentDonationType.getString(RECIP_DON_TYPE_ID));
    } 
Sign up to request clarification or add additional context in comments.

Comments

0

You should do:

JSONArray neededTypesArray = new JSONObject(dataJson).getJSONArray(RECIP_NEEDED_TYPES);

1 Comment

Yeah my bad... Just saw that the root of your json file is an array not an object..
0

=========================================================================

HOW I FIXED THE FOLLOWING ERRORS:

=========================================================================

org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

org.json.JSONException: Value permissions of type java.lang.String cannot be converted to JSONObject

=========================================================================

This might not apply to this particular scenario, but it comes up as a top search result for the given issue/keyword.

So, i bought a script from a professional vendor on codecanyon.

The script consisted of 3x main parts; - MAIN CART SITE (PHP) - MAIN ADMIN SITE + /API (PHP) - ANDROID ADMIN APP (JAVA)

I found many issues once the script was installed. Ranging from incomplete or missing table arrays on the MAIN CART SITE, then i had a problem on the ANDROID ADMIN APP that (upon inspection of logs) revealed a mysqli_exception was to blame.

So after hours of messing around with loops and trying to figure out where the issue was. After actually learning how to dump output to the logs / logcat. I was able to determine that it was in actual fact, a;

BREAKING CHANGE SINCE MYSQL-8

TO FIX, RUN THE FOLLOWING COMMANDS IN mysql TERMINAL;

SET GLOBAL sql_mode = ''; SET SESSION sql_mode = '';

THIS REMOVES THE 'STRICT MODE' amongst other rules that has caused me so much grief over the last few days. Thought i'd better share the answer, hopefully save someone else days of eye-drying torment =].

Remember to reintroduce the default ruleset one rule at a time and test to see what modes your app can support (if this solution fixes your problem) as they are no doubt essential security/data-integrity measures that are there for good reason. Ideally, update codebase to comply with current standards. Unfortunately that's way beyond me at this stage.

Hope this works for you guys.

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

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.