hamidreza haajhoseini hamidreza haajhoseini - 5 months ago 11
JSON Question

use json object with inner objects to android application

This is my json code from url.this objects not have a general objects name but

{H0...H20}
each containing 5 childs that
imgtitle
and
imgtext
too include tow childs.

{
"h0":{
" id ": 1092,
" imgtitle ": {"fa":"cc","en":"dd"},
" imgtext ": {"fa_IR":"zz","en_GB":""},
" url": "www.example.com"
}

"h1":{
" id ":20221,
" imgtitle ":{"fa":"","en":"example"},
" imgtext ":{"fa_IR":"bb","en_GB":""},
" url ":"www.example.com"
}

.
.

"h20" {...
}
}


my problem is
This code does not work. how fix it? and
What can I do if it works for
imgtitle
and
imgtext
??

private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
uid = (TextView)findViewById(R.id.uid);
myurl = (TextView)findViewById(R.id.url);

pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");

}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;

}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {


JSONObject myobject = new JSONObject("h0");

int id = myobject.getInt("id");
String myurl= myobject.getString("url");

uid.setText(""+id);
myurl.setText(myurl);

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

Answer

You are not calling the correct jsonObject, have a look at your code...myObject is new json..so of course it will he empty. You should use json.getJsonObject(h4). I'm answering using mobile so sorry about typos and formatting

Comments