我正在尝试使用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数组eg- [{“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;
}
}
}
答案 0 :(得分:1)
尝试将其实例化为JSONObject
,而不是将解析不是数组而是对象的数据。
JSONObject json = new JSONObject(result);
对于将来的引用,使用正确类型的时间不同:JSONObject
涵盖{}
,JSONArray
涵盖[]
。