6

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

Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117

3 Answers3

8

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());           
  }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • it gives me this in logcat 01-28 13:03:57.329: E/log_tag(1708): Error in http connection java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.zasvadba.mk/test.php 01-28 13:03:57.379: E/log_tag(1708): Error converting result java.lang.NullPointerException: lock == null 01-28 13:03:57.379: E/log_tag(1708): Error parsing data org.json.JSONException: End of input at character 0 of 01-28 13:03:57.379: E/log_tag(1708): Error getJSONfromURL java.lang.NullPointerException – Darko Petkovski Jan 28 '13 at 13:04
  • @BozidarPrcovski : check your URl which you are passing in getJSONfromURL is as `getJSONfromURL("http://www.zasvadba.mk/test.php")` – ρяσѕρєя K Jan 28 '13 at 13:09
  • Yes this is the solution, THANKS ALLOT! – Darko Petkovski Jan 28 '13 at 13:12
3

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

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
1

You can Easily do it using gson library.

Here is the code sample:

Your Entity Class will like:

public class ResponseEntity {
    @SerializedName("id")
    public int id;
    @SerializedName("introtext")
    public String introtext;
    @SerializedName("image")
    public String image;
    @SerializedName("title")
    public String title;
    @SerializedName("catid")
    public String catid;
    @SerializedName("alias")
    public String alias;

}

Now Convert this json Array using GSON library.

Gson gson=new Gson();    
    ResponseEntity[] entities = gson.fromJson(yourResponseAsString.toString(),
          ResponseEntity[].class);

Now you have the entity array at entities.

Thanks.

Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87