I have this array in json code.
$info=array();
while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
array_push($info,$row);
}
$info;
$result_final->lugares_cercanos = $info;
Print this:
{"logstatus":"1","lugares_cercanos":[{"nombre":"Rio Amazonas","distancia":"5119.000"}{"nombre":"Swissotel Quito","distancia":"5823.000"}{"nombre":"Laguna de Yaguarcocha","distancia":"71797.000"}]}
Now, the problem is, How can I put the fields of "lugares_cercanos" into java ArrayList??
I try with this code:
{
JSONArray jdata=post.getserverdata(postparameters2send, URL_connect);
if (jdata!=null && jdata.length() > 0){
JSONObject json_data;
ArrayList<NameValuePair> lugares = new ArrayList<NameValuePair>();
json_data = jdata.getJSONObject(0);
logstatus=json_data.getInt("logstatus");
lugaresCercanos=json_data.getJSONArray("lugares_cercanos");
for (int i = 0; i < lugaresCercanos.length(); ++i) {
JSONObject rec = lugaresCercanos.getJSONObject(i);
String name = rec.getString("nombre");
String dist = rec.getString("distancia");
lugares.add(new BasicNameValuePair(name,dist));
}
}
}
Try this:
JSONObject j = jdata.getJSONObject("obj");
JSONArray jArray = j.getJSONArray("lugares_cercanos");
int len = jArray .length();
for(int i=0; i <len; i++){
String nombre = jArray .getJSONObject(i).optString("nombre");
---------
}
The top level structure (i.e. the JSON string you have posted) is not an array but an object.
I don't know what your post.getserverdata method does, if there is a version that can return a JSONObject you could use:
JSONObject jdata=post.getserverdataobject(postparameters2send, URL_connect);
logstatus=jdata.getInt("logstatus");
lugaresCercanos=jdata.getJSONArray("lugares_cercanos");
...
show this error:
Error parsing data org.json.JSONException: Value {"lugares_cercanos":[{"nombre":"Laguna de Yaguarcocha","distancia":"8686205.000"},{"nombre":"Swissotel Quito","distancia":"8728811.000"},{"nombre":"Rio Amazonas","distancia":"8729333.000"}],"logstatus":"1"} of type org.json.JSONObject cannot be converted to JSONArray
Related
Below is the query which is fetching the data from the MySql Database but the issue is that in browser it successfully outputs the data but in Android app it gives the error which is below
Java.Long.String Cannot Convert to JsonArray
if($stem=$con->prepare("select question from MCQs where c_id=$c_id ")){
$stem->execute ();
$stem->bind_result($question);
$budget=array();
while($stem->fetch())
{
$temp=array();
$temp['question']=$question;
array_push($budget,$temp);
}
echo json_encode($budget);
Below is the Android Side Java Code for the JSON
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("data");
if (success.equals("1")) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String questions = jsonObject1.getString("question").trim();
final String opt0ne = jsonObject1.getString("option1").trim();
;
String opttwo = jsonObject1.getString("option2").trim();
;
String optthree = jsonObject1.getString("option3").trim();
final String correctt = jsonObject1.getString("correct").trim();
JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("data");
for (int i=0;jArray.length(); i++)
{
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
//try this
String msg= jsonObject1.getString("success");
}
I have the following JSON and I would like to parse all of the "vcsapat" and "hcsapat" and all of the datas from the first rows.
Json is here:Link
I tried with the following code, but I only have exceptions
JSONArray JSonAdatok = null;
JSonAdatok = jObject.getJSONArray("vcsapat");
for (int i = 0; i < JSonAdatok.length(); i++) {
JSONObject jo = null;
try {
jo = JSonAdatok.getJSONObject(i);
System.out.print("\n"+jo);
JSONObject kezdojatekosok = jo.getJSONObject("kezdo");
System.out.print("\n"+kezdojatekosok);
for (int j = 0; j < kezdojatekosok.length(); j++) {
JSONObject egyjjson = kezdojatekosok;
And the Exception is the following:
at org.json.JSON.typeMismatch(JSON.java:100)
org.json.JSONObject.getJSONArray(JSONObject.java:588)
Your JSON contains first an Object which starts with "{" so your whole JSON string represents an JSONObject. Then after, get inside it the object "hforma" then the JSONArray of forma, and then you can iterate on them to get on each the hcsapat and vcsapat attributes as strings.
Something like this:
JSONObject complete = new JSONObject(WHOLE_JSON_AS_STRING);
JSONObject hforma = complete.getJSONObject("hforma");
JSONArray forma = hforma.getJSONArray("forma");
for (int i = 0; i < forma.length(); i++) {
JSONObject formaData = forma.getJSONObject(i);
String hcsapat = formaData.getString("hcsapat");
String vcsapat = formaData.getString("vcsapat");
}
I did not test that code, it is just an example that follows the structure of your JSON.
from the following link , I need to get the value stored in "media".
Here is my code:
Content = Content.replace("jsonFlickrFeed(", "");
Content = Content.replace("})", "}");
jsonResponse = new JSONObject(Content);
JSONArray jsonMainNode = jsonResponse.optJSONArray("items"); // this works great!
But I can not access past "items"
You will have to loop through the JSON like this:
...
JSONArray jsonMainNode = jsonResponse.optJSONArray("items");
for (int i=0; i<jsonMainNode.length(); i++) {
String media = jsonMainNode.getJSONObject(i).getString("media");
}
This will loop through the images and return the value(s) in media.
In your case it should be something like this:
..
JSONArray jsonMainNode = jsonResponse.optJSONArray("items");
for (int i=0; i<jsonMainNode.length(); i++) {
JSONObject finalNode = jsonMainNode.getJSONObject(i);
JSONArray finalArray = finalNode.optJSONArray("media");
for (int j=0; j<finalArray.length(); j++) {
String m = finalArray.getJSONObject(j).getString("m");
}
}
...because there is another node inside the media node, called m.
Here is an example getting the tags string for each item in the JSONArray that you have:
for (int i = 0; i < jsonMainNode.length(); i++){
JSONObject jsonObject = jsonMainNode.getJSONObject(i);
String tags = jsonObject.getString("tags");
}
This will iterate through all JSONObjects that are in the array, and extract the tags field from each object.
JSONArray jsonMainNode = jsonResponse.optJSONArray("items"); // this works great!
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject item = (JSONObject) jsonMainNode.get(i);
JSONObject media = item.getJSONObject("media");
String valueMedia = media.getString("m");
Log.d("TAG", valueMedia);
}
I have a webservice which returns a JSON array in this format:
[{"imageid":"3","userid":"1","imagepath":"SLDFJNDSKJFN","filterid":"1","dateadded":"2014-05-06 21:20:18.920257","public":"t"},
{"imageid":"4","userid":"1","imagepath":"dsfkjsdkfjnkjdfsn","filterid":"1","dateadded":"2014-05-06 21:43:37.642748","public":"t"}]
I need to get all the attributes seperately? How would I do this?
I know how to do it with JSONObject if there is just 1 thing being returned, but how does it work when multiple items are returned?
Thanks
try {
JSONArray jArray = new JSONArray(jsonString);
String s = new String();
for (int i = 0; i < jArray.length(); i++) {
s = jArray.getJSONObject(i).getString("imageid").toString();
s = jArray.getJSONObject(i).getString("userid").toString();
}
} catch (JSONException je) {
}
Create an Object class with all variables, create a List for this Object, add all objects in your JSONArray to the list, use the one you need.
List<YourObject> objList = new ArrayList<YourObject>();
JSONArray a = new JSONArray(response);
int size = a.length();
for (int i=0 ; i<size ; i++){
JSONObject aa = a.getJSONObject(i);
String id = aa.getString("imageid");
String userid = aa.getString("userid");
String imagepath = aa.getString("imagepath");
String filterid = aa.getString("filterid");
String dateadded = aa.getString("dateadded");
String publicText = aa.getString("public");
YourObject obj = new YourObject(id,userid,imagepath,filterid,dateadded,publicText);
objList.add(obj);
}
So what you are having here is some JSON objects inside a JSON array.
What you want to do is this:
JSONArray array = ...;
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
// Extract whatever you want from the JSON object.
}
I hope it helped.
You can use JSONArray to parse array of JSON response.
private void parseJsonArray(String response) {
try {
JSONArray array = new JSONArray(response);
for(int i=0;i<array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
String ImageId = jsonObject.getString("imageid");
Log.v("JSON Parser", "ImageId: "+ImageId);
}
} catch (Exception e) {
e.printStackTrace();
}
}
I have some JSON with the following structure:
{"cit": [
"ALL",
"Aceh Barat",
"Aceh Besar",
"Aceh Jaya",
"Aceh Selatan",
"Aceh Tengah",
"Aceh Timur",
"Aceh Utara"]}
i have try to parsing my json like this :
JSONObject jsonObject = new JSONObject(result);
JSONArray city=jsonObject.getJSONArray("cit");
for (int j=0; j < city.length(); j++){
JSONObject cit = city.getJSONObject(j);
String kot = cit.toString(j);
kota.add(kot);
}
on post execute :
ArrayAdapter<String> SpinnerKota = new ArrayAdapter<String>(Pencarian.this, android.R.layout.simple_list_item_1, kota);
spin_kota.setAdapter(SpinnerKota);
but nothing happen, is there any wrong with my code? i hope someone can help me to solve my problem.
"cit": [ // json array cit
"ALL", // index 0 is ALL
Also there is no json object inside json array cit. So you don't need this JSONObject cit = city.getJSONObject(j).
Change
String kot = cit.toString(j);
To
String kot = (String) city.get(j);
Use the below
JSONObject jsonObject = new JSONObject("myjson string");
JSONArray city=jsonObject.getJSONArray("cit");
for(int i=0;i<city.length();i++)
{
String cities = (String) city.get(i);
Log.i("All Cities",cities);
kota.add(cities);
}
Do it like this
JSONArray json = jParser.getJSONFromUrl(URL);
JSONObject c = json.getJSONObject(0);
JSONArray city = c.getJSONArray("cit");
for (int j=0; j < city.length(); j++){
String kot = cit.get(j);
kota.add(kot);
}
Object obj = yourJsonResult;
JSONObject jsonObject = (JSONObject) obj;
JSONArray msg = (JSONArray) jsonObject.get("cit");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
you can do it as following code this is tested
String jsonSource="{'cit': ['ALL','Aceh Barat','Aceh Besar','Aceh Jaya','Aceh Selatan','Aceh Tengah','Aceh Timur','Aceh Utara']}";
try {
JSONObject jsonObject=new JSONObject(jsonSource);
JSONArray jsonArray=jsonObject.getJSONArray("cit");
int length = jsonArray.length();
String [] cities = new String [length];
if (length > 0) {
for (int i = 0; i < length; i++) {
cities[i] = jsonArray.getString(i);
}
}
//to check we can print our string array
for (int i = 0; i < cities.length; i++) {
Log.d("JSON parsing ",cities[i]);
}
} catch (JSONException e) {
e.printStackTrace();
}