0

我正在尝试使用 JSONArray 解析字符串。我在 logcat 中收到警告:

org.json.JSON.typeMismatch(JSON.java:107)
org.json.JSONArray.<init>(JSONArray.java:91)
org.json.JSONArray.<init>(JSONArray.java:103)

我的代码:

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);


mylist = new ArrayList<HashMap<String, String>>();
map = new HashMap<String, String>();
try {

    JSONArray arr = new JSONArray(result);

        for (int i = 0; i < arr.length(); i++) {

             JSONObject jObj = arr.getJSONObject(i);
            c_id = jObj.getString(CID);
            j_make = jObj.getString(MAKE);
            j_model = jObj.getString(MODEL);
            j_version = jObj.getString(VERSION);
            j_price = jObj.getString(PRICE);
            j_reg_plc = jObj.getString(PLACE_REG);

            data = "Make: " + j_make + "\nModel: " + j_model
                           + "\nVersion: " + j_version + "\nPrice: " + j_price
                   + "\nCity: " + j_reg_plc;

            map.put("car", data);
            mylist.add(map);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    list = (ListView) findViewById(R.id.listView1);

    String[] from = new String[] { "car" };
    int[] to = new int[] { R.id.text1 };
    ListAdapter adapter = new SimpleAdapter(view_cars.this, mylist, 
                          R.layout.text_adaptr, from, to);


}

我对 wats 错误一无所知,因为相同的代码以不同的意图工作。

- - - 更新 - - -

我已经克服了警告。这是一个脚本错误。但代码仍然无法正常工作。

我收到一个 json 数组,例如 - [{"car_id":"22","model":"model1"}] 等

我的完整代码

public class view_cars extends ListActivity {

public String s_id, ID, is, str;
String FILENAME = "http://animsinc.com/viewCars.php";

ListView list;
String data;

ArrayList<HashMap<String, String>> mylist;

HashMap<String, String> map;

static String j_id = null;
static Object j_make = null;
static String j_model = null;
static String j_version = null;
static String j_price = null;
static String j_reg_plc = null;

String CID = "car_id";
String MAKE = "make";
String MODEL = "model";
String VERSION = "version";
String PRICE = "expected_price";
String PLACE_REG = "registration_place";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_prof);

    Bundle bundle = getIntent().getExtras();
    String stuff = bundle.getString("stuff");

    list = getListView();
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

    Toast.makeText(this, "try " + stuff, 1000).show();


    try {

        JSONArray jArray = new JSONArray(stuff.toString());

        for (int i = 0; i < jArray.length(); i++) {

            JSONObject jObject = jArray.getJSONObject(i);
            s_id = jObject.getString("seller_id");
            // s_id = jObject.getString(ID);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Toast.makeText(this, "JSON: " + s_id, 1000).show();

    startDownload();

}


@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    Intent CheckProfile = new Intent(this, MenuPg.class);
    startActivity(CheckProfile);

}

private void startDownload() {

    new AppTask().execute(FILENAME);
}

public class AppTask extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

         Toast.makeText(view_cars.this, ""+ result, 1000).show();
        // display(is);

        // void display(String res){

        str = result;

        mylist = new ArrayList<HashMap<String, String>>();
        map = new HashMap<String, String>();
        try {
            Toast.makeText(getApplicationContext(),"Inside Try", Toast.LENGTH_LONG).show();
            JSONArray jJArray = new JSONArray(str);

            for (int i = 0; i < jJArray.length(); i++) {



                JSONArray e = jJArray.getJSONArray(i);


                JSONObject jObject = jJArray.getJSONObject(i);
                j_id = jObject.getString(ID);

                j_make = jObject.getString(MAKE);

                j_model = jObject.getString(MODEL);

                j_version = jObject.getString(VERSION);
                j_price = jObject.getString(PRICE);
                j_reg_plc = jObject.getString(PLACE_REG);

                data = "Make: "+ j_make + "\nModel: " + j_model + "\nVersion: " 
                                                 + j_version  + "\nPrice: " + j_price
                        + "\nCity: " + j_reg_plc;

                Toast.makeText(getApplicationContext(),""+data, Toast.LENGTH_LONG).show();

                map.put("car", data);

                mylist.add(map);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }




    //  list = (ListView) findViewById(R.id.listView1);
        Toast.makeText(getApplicationContext(),""+mylist, Toast.LENGTH_LONG).show();
        String[] from = new String[] { "car"};
        int[] to = new int[] { R.id.text1 };
        ListAdapter adapt = new SimpleAdapter(view_cars.this, mylist,
                R.layout.text_adaptr, from, to);

        list.setAdapter(adapt);

        // Assign adapter to ListView list.setAdapter(adapter);




    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(String... params) {
        // String is = null;
        // str1 = et1.getText().toString();
        // str2 = et2.getText().toString();


        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(FILENAME);
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    1);
            nameValuePairs.add(new BasicNameValuePair("s_id", s_id));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = EntityUtils.toString(entity);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

        return is;
    }
}

}
4

1 回答 1

1

尝试将其实例化为 aJSONObject而不是您解析的数据不是数组而是对象。

JSONObject json = new JSONObject(result);

对于将来的参考,不同的时候使用正确的类型是:JSONObject被覆盖{}JSONArray被覆盖[]

于 2013-04-09T12:19:42.470 回答