i am getting this error
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
this is my json data that i am trying to parse.`[["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"]]
this is my code snippet to parse the json data.
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);
}
`
Its json array of arrays. It is like a 2D array.
JsonArray jsonarray= new JsonArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
strListView[i] = jsonarray.getJsonArray(i);
}
Link Source: https://www.json.com/json-array
JSONObject json_data = jsonArray.getJSONObject(i);
Here ' jsonArray' contains array object not json object. So you have to change that to JsonArray
JSONArray jsonArrayElement = jsonArray.getJSONArray(0);
And also this json is valid but not conventional or useful..!!
Check more information for json here .
Try by changing the lines
JSONObject json_data = jsonArray.getJSONObject(i);
strListView[i] = json_data.getString("Num_Rows");
to
JSONArray innerJsonArray = jsonArray.getJSONArray(i);
strListView[0] = json_data.get(0);
Try this. working fine
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.