Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse json array android

Hi i'm trying to parse json array from this url. The json array looks like this.

    [
   {
      "id":1,
      "introtext":"\u041b\u0438\u043c\u0443\u0437\u0438\u043d\u0430\u0442\u0430 \u0435 \u043e\u0434 \u0430\u043c\u0435\u0440\u0438\u043a\u0430\u043d\u0441\u043a\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u0432\u043e \u0431\u0435\u043b\u0430 \u0431\u043e\u0458\u0430 \u0434\u043e\u043b\u0433\u0430 \u043e\u043a\u043e\u043b\u0443 8,5 \u043c\u0435\u0442\u0440\u0438. \u041e\u043f\u0440\u0435\u043c\u0435\u043d\u0430 \u0435 \u0441\u043e \u043a\u043b\u0438\u043c\u0430 \u0440\u0435\u0434, \u0422\u0412, \u0414\u0412\u0414 \u0438 \u0431\u0430\u0440. \u041c\u043e\u0436\u0430\u0442 \u0434\u0430 \u0441\u0435 \u0432\u043e\u0437\u0430\u0442 \u0434\u043e 9 \u043b\u0438\u0446\u0430. \u0421\u0435 \u0438\u0437\u043d\u0430\u0458\u043c\u0443\u0432\u0430 \u0441\u043e \u043d\u0430\u0448 \u0448\u043e\u0444\u0435\u0440.\n{AdmirorGallery}..\/katalog\/prevoz\/limo-servis-jasmina\/linkoln\/{\/AdmirorGallery}\n\u00a0",
      "image":"http:\/\/zasvadba.mk\/media\/k2\/items\/cache\/787ae9ec9023a82f5aa7e4c1a64f73cb_S.jpg",
      "title":"\u041b\u0438\u043c\u0443\u0437\u0438\u043d\u0430 \u041b\u0438\u043d\u043a\u043e\u043b\u043d",
      "catid":"20",
      "alias":"\u043b\u0438\u043c\u0443\u0437\u0438\u043d\u0430-\u043b\u0438\u043d\u043a\u043e\u043b\u043d-\u043b\u0438\u043c\u043e-\u0441\u0435\u0440\u0432\u0438\u0441-\u0458\u0430\u0441\u043c\u0438\u043d\u0430"
   }
]

I'm doing this in my java class

try {
            JSONfunctions j=new JSONfunctions();
            JSONObject json = j.getJSONfromURL(url);
            Log.i("log_tag", json.toString()); 
            String jsonvalues =  json.getString("id");

            Log.i("DARE", jsonvalues);  
        }
        catch (Exception ex)
        {
            Log.e("log_tag", "Error getJSONfromURL "+ex.toString());           
        }
    }

But it doesn't work, can anybody help me parse my json array

like image 902
Darko Petkovski Avatar asked Jan 28 '13 12:01

Darko Petkovski


2 Answers

you will need to make two changes in your current code according to string u have posted here for parsing as Json :

First : change the return type of getJSONfromURL method to JSONArray and return JSONArray from it instead of JSONObject For example :

public JSONArray getJSONfromURL(String url){
    String str_response="response from server";

    // convert response to JSONArray
   JSONArray json_Array=new JSONArray(str_response);

   return json_Array; //<< retun jsonArray
}

Second : change your code as for getting value from JsonArray :

try {
    JSONfunctions j=new JSONfunctions();
    JSONArray jArray = j.getJSONfromURL(url);
    Log.i("log_tag", jArray.toString()); 

     for(int i=0;i<jArray.length();i++){
      JSONObject json_data = jArray.getJSONObject(i);
      String jsonvalues =  json_data.getString("id");
      // .. get all value here
      Log.i("DARE", jsonvalues);  
   }
  }
catch (Exception ex)
   {
    Log.e("log_tag", "Error getJSONfromURL "+ex.toString());           
  }
like image 124
ρяσѕρєя K Avatar answered Oct 16 '22 14:10

ρяσѕρєя K


Of course it doesn't work! You should use json.getJSONArray(...) method for parsing arrays in Json :)

like image 40
Andremoniy Avatar answered Oct 16 '22 14:10

Andremoniy