gpt4 book ai didi

java - org.json.JSONException : Valuef type java. lang.String 无法转换为 JSONObject

转载 作者:行者123 更新时间:2023-12-02 13:03:29 24 4
gpt4 key购买 nike

所以我试图将 JSONArray 中的值获取到 JSONObject 但我不断收到此错误:org.json.JSONException:java.lang.String 类型为 0 的值 Aamod Shoghi 无法转换为 JSONObject

JSONObject json = jParser.makeHttpRequest(url_all_resorts, "POST", params);
Log.d("All Resorts : ", json.toString());
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// resorts found
// Getting Array of resorts
resorts = json.getJSONArray(TAG_RESORT);
rates = json.getJSONArray(TAG_PRICE);

int i;

// looping through All resorts
for (i = 0; i < resorts.length() & i < rates.length(); i++) {
JSONObject c = resorts.getJSONObject(i);
JSONObject d = rates.getJSONObject(i);

// Storing each json item in variable
String name = c.getString(TAG_NAME);
String price = d.getString(TAG_ROOM_PRICE);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_ROOM_PRICE, price);

// adding HashList to ArrayList
resortsList.add(map);
}

}

JSON 示例数据

{"resorts":["Aamod Shoghi","Aamod Shoghi","Aamod Shoghi"],"room_prices":["100","800","1000"],"success":1}

最佳答案

您的resortsroom_prices数组仅包含字符串

用途:

// Storing each json item in variable
String name = resorts.getString(i);
String price = rates.getString(i);

而不是:

JSONObject c = resorts.getJSONObject(i);
JSONObject d = rates.getJSONObject(i);

// Storing each json item in variable
String name = c.getString(TAG_NAME);
String price = d.getString(TAG_ROOM_PRICE);

试试这个:

        ...........
...................

if (success == 1) {
// resorts found
// Getting Array of resorts
resorts = json.getJSONArray("resorts");
rates = json.getJSONArray("room_prices");

int i;

// looping through All resorts
for (i = 0; i < resorts.length() & i < rates.length(); i++) {
// Storing each json item in variable
String name = resorts.getString(i);
String price = rates.getString(i);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_ROOM_PRICE, price);

// adding HashList to ArrayList
resortsList.add(map);
}

}

关于java - org.json.JSONException : Valuef type java. lang.String 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44231402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com