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

bprjcwpo  于 2023-02-06  发布在  Android
关注(0)|答案(3)|浏览(120)
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();
        }
    }
svgewumm

svgewumm1#

使这个改变你的代码

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

bvuwiixz2#

您应该:
JSONArray neededTypesArray = new JSONObject(dataJson).getJSONArray(RECIP_NEEDED_TYPES);

mutmk8jj

mutmk8jj3#

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

    • 如何修复以下错误:**

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

组织. json. JSON异常:java. lang. String类型的值无法转换为JSONObject
组织. json. JSON异常:类型为java. lang. String的值权限无法转换为JSONObject

=========================================================================
这可能不适用于此特定场景,但它会作为给定问题/关键字的顶级搜索结果出现。
所以,我从codecanyon上的一个专业卖家那里买了一个剧本。
剧本由3个主要部分组成;- 主购物车网站(PHP)-主管理网站+/API(PHP)-安卓管理应用程序(JAVA)
安装脚本后,我发现了许多问题,从主购物车站点上的表数组不完整或丢失,然后我在ANDROID ADMIN应用程序上遇到了一个问题,(在检查日志后)显示mysqli_exception是罪魁祸首。
因此,在花了几个小时摆弄循环并试图找出问题所在之后,在实际学习了如何将输出转储到logs/logcat之后,我能够确定它实际上是一个;
自MYSQL-8以来的突破性变化
要修复,请在mysql终端运行以下命令;
设置全局sql_mode ="";设置会话sql_mode ="";
这删除了"严格模式"在其他规则中,已经引起了我这么多的悲伤在过去几天.我想我最好分享答案,希望节省别人的眼睛干燥的折磨天=].
记得重新引入默认规则集,一次一个规则,并测试你的应用程序可以支持哪些模式(如果这个解决方案解决了你的问题),因为它们无疑是必要的安全/数据完整性措施,有很好的理由。理想情况下,更新代码库,以符合当前的标准。不幸的是,这远远超出了我在这个阶段。
希望这对你们有用。

相关问题