我收到此错误
Error in parsing Dataorg.json.JSONException: Value ["AACHI"] at 0 of type org.json.JSONArray cannot be converted to JSONObject
Process: info.androidhive.materialdesign, PID: 18373
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
这是我试图解析的json数据。[[" AACHI"],[" AJAY COMPANY"],[" ALL OUT" ],[" AMBICA"],[" AMICO PRODUCTS"],[" AMUL"],[" ANAND BHOG PRODUCTS"],[ " ANNAPURNA"],[" ANOOS PRODUCTS"],[" ARVIND LABORATORIES"],[" ASWINI PHARMACEUTICALS"],[&# 34; ATTK PRODUCTS"],[" AVA"],[" BAJAJ PRODUCTS"],[" BAMBINO"],[" BANJARAS产品"],[" BHAGAYALAKSHIMI PRODUCTS"],[" BRITANNIA"],[" cadbury"]]
这是我解析json数据的代码片段。
class DownloadJson extends AsyncTask {
Activity context;
ListView myListView;
private ProgressDialog dialog = new ProgressDialog(getActivity());
public DownloadJson(Activity context, ListView myListView) {
this.myListView = myListView;
this.context = context;
}
public DownloadJson() {
}
@Override
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected Object doInBackground(Object[] params) {
String result = null;
InputStream isr = null;
String imageId = null;
String ip = "http://ganarajsshetti.tk/mobileapp/selectjson.php/";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(ip);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
isr = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http Connection" + e.toString());
}
// converting response to string
try {
BufferedReader br = new BufferedReader(new InputStreamReader(isr));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error in Converting Data" + e.toString());
}
// parse JSON data
try {
JSONArray jsonArray = new JSONArray(result);
strListView = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json_data = jsonArray.getJSONObject(i);
strListView[i] = json_data.getString("Num_Rows");
System.out.println("--------------"+json_data.getString("Num_Rows"));
Log.e("ACK_tag", "DATA" + strListView[i]);
}
} catch (Exception e) {
Log.e("log_tag", "Error in parsing Data" + e.toString());
}
return null;
}
@Override
protected void onPostExecute(Object o) {
//objAdapter = new ArrayAdapter<String>(context,R.layout.vendorwithimage,R.id.venderdescrip,strListView);
if (dialog.isShowing()) {
dialog.dismiss();
}
callCustomAdaper(context);
}
}
@Override
public void onStop() {
super.onStop();
if (task.getStatus() == AsyncTask.Status.RUNNING)
task.cancel(true);
if (task.getStatus() == AsyncTask.Status.RUNNING)
task.cancel(true);
}
`
答案 0 :(得分:2)
它的json数组数组。它就像一个2D阵列。
JsonArray jsonarray= new JsonArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
strListView[i] = jsonarray.getJsonArray(i);
}
答案 1 :(得分:1)
JSONObject json_data = jsonArray.getJSONObject(i);
这里&#39; jsonArray&#39;包含数组对象而不是json对象。 所以你必须把它改成JsonArray
JSONArray jsonArrayElement = jsonArray.getJSONArray(0);
此json也有效,但不是常规或有用.. !!
查看json here的更多信息。
答案 2 :(得分:0)
尝试更改行
JSONObject json_data = jsonArray.getJSONObject(i);
strListView[i] = json_data.getString("Num_Rows");
到
JSONArray innerJsonArray = jsonArray.getJSONArray(i);
strListView[0] = json_data.get(0);
答案 3 :(得分:0)
试试这个。工作正常
String json = "[[\"AACHI\"],[\"AJAY COMPANY\"],[\"ALL OUT\"],[\"AMBICA\"],[\"AMICO PRODUCTS\"],[\"AMUL\"],[\"ANAND BHOG PRODUCTS\"],[\"ANNAPURNA\"],[\"ANOOS PRODUCTS\"],[\"ARVIND LABORATORIES \"],[\"ASWINI PHARMACEUTICALS\"],[\"ATTK PRODUCTS\"],[\"AVA\"],[\"BAJAJ PRODUCTS \"],[\"BAMBINO\"],[\"BANJARAS PRODUCTS\"],[\"BHAGAYALAKSHIMI PRODUCTS\"],[\"BRITANNIA\"],[\"cadbury\"]]";
try {
JSONArray jsAr = new JSONArray(json);
String job="";
for(int i=0;i<jsAr.length();i++)
{
JSONArray js1 = jsAr.getJSONArray(i);
job += js1.getString(0);
}
Toast.makeText(this,job,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(this,"Error",Toast.LENGTH_LONG).show();
e.printStackTrace();
}