org.json.JSONException:类型java.lang.String的值无法转换为JSONArray

时间:2015-04-10 19:51:53

标签: android arrays json jsonexception

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

我在尝试获取时遇到此错误JSONArray neededTypesArray 如果你有任何想法如何解决它,我将非常感激!

这是我的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},
    ...]

这是我的类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();
        }
    }

2 个答案:

答案 0 :(得分:3)

将此更改为您的代码

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

答案 1 :(得分:0)

你应该这样做:

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