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

时间:2014-02-25 10:16:22

标签: android json android-intent

获取字符串无法转换为JSONObject

见下面的代码:

MainAdapter.java: -

 HashMap<String, String> resultp = new HashMap<String, String>();

// Capture ListView item click
        itemView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Get the position
                resultp = data.get(position);

                Intent intent = new Intent(context, AnotherActivity.class);
                // Pass all data rank
                intent.putExtra("name", resultp.get(MainActivity.NAME));
                context.startActivity(intent);
            }
        });
        return itemView;

AnotherActivity.java: -

            String value = getIntent().getStringExtra("name");

            jsonobject =  new JSONObject(value);

2 个答案:

答案 0 :(得分:1)

我得到了@Raghunandan提供的解决方案

     JSONObject jb;
            try {
                Intent intent = new Intent(context, AnotherActivity.class);
                jb = MainActivity.jsonarray.getJSONObject(position);
                intent.putExtra("name",jb.toString());
                context.startActivity(intent);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

答案 1 :(得分:0)

 String value = getIntent().getStringExtra("name");
 jsonobject =  new JSONObject(value);

除非value不是有效的JSONObject,否则以下行应该有效。检查this是否有有效的JSON格式