我是android开发的新手。我遇到了一个错误: - JSON对象无法在jsonArray中转换: -
我的ProductInner.java:
public class ProductInner extends AppCompatActivity {
TextView textView;
ImageView imageView;
LinearLayout linearLayout;
String s,s1;
List<String> mainImage = new ArrayList<>();
List<String> outerlist = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_inner);
textView = (TextView)findViewById(R.id.product_inner_page_product_name);
imageView = (ImageView)findViewById(R.id.product_inner_page_product_main_image);
linearLayout = (LinearLayout)findViewById(R.id.sub_images_show_section);
new ProductInnerDisplay().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/product&id=80&key=test123$");
}
public class ProductInnerDisplay extends AsyncTask<String,Void,Void>{
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(ProductInner.this);
dialog.show();
}
@Override
protected Void doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if(status==200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(data);
JSONArray jsonArray = jsonObject.getJSONArray("rproducts");
JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
List<String> list = new ArrayList<>();
list.add(jsonObject1.getString("product_id"));
list.add(jsonObject1.getString("thumb"));
list.add(jsonObject1.getString("name"));
list.add(jsonObject1.getString("price"));
list.add(jsonObject1.getString("rating"));
list.add(jsonObject1.getString("reviews"));
list.add(jsonObject1.getString("href"));
Log.d("json parse","");
}
for(int i=0;i<jsonArray1.length();i++){
Log.d("length of",jsonArray1.length()+"");
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
List<String> list = new ArrayList<>();
list.add(jsonObject1.getString("id"));
// list.add(jsonObject1.getString("name"));
s=jsonObject1.getString("name");
list.add(jsonObject1.getString("model"));
list.add(jsonObject1.getString("reward"));
list.add(jsonObject1.getString("points"));
// list.add(jsonObject1.getString("image"));
outerlist.add(jsonObject1.getString("image"));
JSONArray jsonArray2 = jsonObject1.getJSONArray("images");
for(int j=0;j<jsonArray2.length();j++){
List<String> list1 = new ArrayList<>();
Log.d("i am here","");
mainImage.add(jsonArray2.getString(j));
Log.d("i am here next", "");
// list1.add(jsonArray2.getString(j));
}
list.add(jsonObject1.getString("price"));
Log.d("i am here 1", "");
JSONArray jsonArray3 = jsonObject1.getJSONArray("options");
for(int j=0;j<jsonArray3.length();j++){
JSONObject jsonObject2 = jsonArray3.getJSONObject(j);
List<String> list1 = new ArrayList<>();
list1.add(jsonObject2.getString("product_option_id"));
list1.add(jsonObject2.getString("option_id"));
list1.add(jsonObject2.getString("name"));
list1.add(jsonObject2.getString("type"));
JSONArray jsonArray4 = jsonObject2.getJSONArray("option_value");
for(int k=0;k<jsonArray4.length();k++){
JSONObject jsonObject3 = jsonArray4.getJSONObject(k);
List<String> list2 = new ArrayList<>();
list2.add(jsonObject3.getString("product_option_value_id"));
list2.add(jsonObject3.getString("option_value_id"));
list2.add(jsonObject3.getString("name"));
list2.add(jsonObject3.getString("image"));
list2.add(jsonObject3.getString("price"));
list2.add(jsonObject3.getString("price_prefix"));
}
list1.add(jsonObject2.getString("required"));
}
list.add(jsonObject1.getString("minimum"));
list.add(jsonObject1.getString("newprice"));
list.add(jsonObject1.getString("qty"));
list.add(jsonObject1.getString("rating"));
list.add(jsonObject1.getString("description"));
JSONArray jsonArray4 = jsonObject1.getJSONArray("attribute_groups");
for(int j=0;j<jsonArray4.length();j++){
JSONObject jsonObject2 = jsonArray4.getJSONObject(j);
List<String> list1 = new ArrayList<>();
list1.add(jsonObject2.getString("attribute_group_id"));
list1.add(jsonObject2.getString("name"));
JSONArray jsonArray5 = jsonObject2.getJSONArray("attribute");
for(int k=0;k<jsonArray5.length();k++){
JSONObject jsonObject3 = jsonArray5.getJSONObject(k);
List<String> list2 = new ArrayList<>();
list2.add(jsonObject3.getString("attribute_id"));
list2.add(jsonObject3.getString("name"));
list2.add(jsonObject3.getString("text"));
}
}
}
}
}catch (IOException |JSONException e){
Log.e("Error",e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
dialog.dismiss();
// super.onPostExecute(aVoid);
// new SetImageView(image).execute(thumbDiscount.get(j));
textView.setText(s);
new SetImage(imageView).execute(s1);
for(int i=0;i<mainImage.size();i++){
ImageView imageView = new ImageView(ProductInner.this);
new SetImage(imageView).execute(mainImage.get(i));
linearLayout.addView(imageView, i);
}
}
}
public class SetImage extends AsyncTask<String,Void,Bitmap>{
ImageView bitmap;
public SetImage(ImageView bitmap){
this.bitmap = bitmap;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... params) {
String urldisplay = params[0];
Bitmap bitmap = null;
try{
InputStream in = new java.net.URL(urldisplay).openStream();
bitmap = BitmapFactory.decodeStream(in);
}catch (IOException e ) {
e.getMessage();
// Log.e("Error :",e.getMessage());
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
bitmap.setImageBitmap(result);
//super.onPostExecute(bitmap);
}
}
}
我的Logcat显示此错误: -
E /错误:值{“id”:“80”,“name”:“Bag_80”,“model”:“
”, “奖励”: “0”, “分”: “0”, “图像”: “http://opencart.codeniques.com/shopping/image/cache/data/product/bag_81_1-1000x1000.JPG”, “图像”:[ “http://opencart.codeniques.com/shopping/image/cache/data/product/bag_80_2-80x80.JPG”], “价格”:1175, “最小”: “1”,“newprice “:1163.25,” 数量 “:4”,等级 “:0,” 说明 “:”“} 在org.json.JSONObject类型的productdata中无法转换为 JSONArray`
答案 0 :(得分:3)
productdata
不是JSONArray而是JSONObject,所以你必须改变
JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");
到
JSONObject jsonObject1 = jsonObject.getJSONObjct("productdata");
答案 1 :(得分:1)
与日志一样:
productdata of type org.json.JSONObject cannot be converted to JSONArray
表示productdata
密钥用于响应服务器json的JSONObject
而不是JSONArray
。
因此,将productdata
键值作为JSONObject而不是JSONArray:
JSONObject jsonObject1 = jsonObject.getJSONObject("productdata");
答案 2 :(得分:0)
您正在将productData作为JSONArray获取,而它是一个包含JsonArray图像的JsonObject,我在您提供的json链接中可以看到
JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");
该代码行应该是JsonObject