Dostávam túto chybu
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
toto sú moje údaje, ktoré sa snažím analyzovať.`[[" AACHI "], [" AJAY COMPANY "], [" ALL OUT "], [" AMBICA "], [" AMICO PRODUKTY "], [" AMUL "], [" ANAND BHOG PRODUCTS "], [ „ANNAPURNA“], [„ANOOS PRODUKTY“], [„ARVINDOVÉ LABORATÓRIÁ“], [„ASWINI LIEČIVÁ“], [„ATTKOVÉ VÝROBKY“], [„AVA“], [„BAJAJOVÉ VÝROBKY“], [„BAMBINO“ “ ], ["BANJARAS PRODUKTY"], ["BHAGAYALAKSHIMI PRODUKTY"], ["BRITANNIA"], ["cadbury"]]]
toto je môj zlomok kódu na analýzu údajov 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);
}
`
odpovede:
0 pre odpoveď č. 1Skúste zmeniť riadky
JSONObject json_data = jsonArray.getJSONObject(i);
strListView[i] = json_data.getString("Num_Rows");
na
JSONArray innerJsonArray = jsonArray.getJSONArray(i);
strListView[0] = json_data.get(0);
2 pre odpoveď č. 2
Je to pole polí. Je to ako 2D pole.
JsonArray jsonarray= new JsonArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
strListView[i] = jsonarray.getJsonArray(i);
}
Zdroj odkazu:https://www.json.com/json-array
1 pre odpoveď č. 3
JSONObject json_data = jsonArray.getJSONObject (i);
"JsonArray" obsahuje objekt poľa, nie objekt json. Musíte to zmeniť na JsonArray
JSONArray jsonArrayElement = jsonArray.getJSONArray(0);
A tiež to json je platný, ale nie konvenčný alebo užitočný .. !!
Skontrolujte viac informácií pre json tu.
0 pre odpoveď č. 4
Skúste to. funguje dobre
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();
}