gpt4 book ai didi

android - 在android中用jsonarray解析json对象

转载 作者:搜寻专家 更新时间:2023-11-01 09:44:41 25 4
gpt4 key购买 nike

你好你好我是 android 的新手。我正在从服务器收到响应。但我得到以下回应..

出现以下错误

Value {"Elec":"Cup1"} at 1 of type java.lang.String cannot be converted to JSONObject

响应如下

{"Electronics":["{\"Elec\":\"Cup0\"}","{\"Elec\":\"Cup1\"}","{\"Elec\":\"Cup2\"}","{\"Elec\":\"Cup3\"}","{\"Elec\":\"Cup4\"}","{\"Elec\":\"Cup5\"}","{\"Elec\":\"Cup6\"}","{\"Elec\":\"Cup7\"}","{\"Elec\":\"Cup8\"}","{\"Elec\":\"Cup9\"}"],"Printable":["{\"Print\":\"Mug0\"}","{\"Print\":\"Mug1\"}","{\"Print\":\"Mug2\"}","{\"Print\":\"Mug3\"}","{\"Print\":\"Mug4\"}","{\"Print\":\"Mug5\"}","{\"Print\":\"Mug6\"}","{\"Print\":\"Mug7\"}","{\"Print\":\"Mug8\"}","{\"Print\":\"Mug9\"}"]}

我发送和接收的响应如下

    class SliderImages extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... urls) {

return POST1(urls[0]);
}

@Override
public void onPostExecute(String result) {

}

}

public String POST1(String url) {
InputStream inputStream;
String result = "";
try {
arraylist = new ArrayList<HashMap<String, String>>();

// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();

// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);

String json = "";

// 3. build jsonObject
JSONObject jsonObject = new JSONObject();


// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
Log.d("JsonStringSend", json);
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);

// 5. set json to StringEntity
StringEntity se = new StringEntity(json);

// 6. set httpPost Entity
httpPost.setEntity(se);

// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);

// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();

// 10. convert inputstream to string
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
Log.d("JSONResponce", result);
JSONObject jsonObj = new JSONObject(result);
contacts = jsonObj.getJSONArray("Electronics");
for (int i = 1; i < contacts.length()-1; i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("Print");
Log.e("Errrrrrrr",""+id);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
// 11. return result
return result;
}


private String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;

}

所以得到以下错误

Value {"Elec":"Cup1"} at 1 of type java.lang.String cannot be converted to JSONObject

请帮帮我

提前致谢

最佳答案

这是在 jsoneditoronline.org 中格式化你的 json 后的结果

enter image description here

请注意,“Electronics”数组中的对象不是 JSONObject,而是 String

一个简单的解决方法是首先将对象作为 String 获取,然后将其解析为 JSONObject,例如:

    // 10. convert inputstream to string
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
Log.d("JSONResponce", result);
JSONObject jsonObj = new JSONObject(result);
contacts = jsonObj.getJSONArray("Electronics");

// Note contacts.length() NOT contacts.length() - 1
for (int i = 1; i < contacts.length(); i++) {

String cString = contacts.getString(i);
JSONObject c = new JSONObject(cString);

String id = c.getString("Print");
Log.e("Errrrrrrr",""+id);
}
}

然而,最好的选择是更正从服务器返回的数据。

关于android - 在android中用jsonarray解析json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38538152/

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