Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
1  
it doesn't work it would help if you elaborate these three words – Abu Jan 28 at 12:09
@Abu means that doesnt return any expected results – Bozidar Prcovski Jan 28 at 12:13
you can put the results you are getting and the result that you are expecting, then it will help to understand the problem – Abu Jan 28 at 12:15
@BozidarPrcovski : problem solved or not? – ρяσѕρєя K Jan 28 at 12:43
@ρяσѕρєяK still not – Bozidar Prcovski Jan 28 at 12:46
show 1 more comment

3 Answers

up vote 1 down vote accepted

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());           
  }
share|improve this answer
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 – Bozidar Prcovski Jan 28 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 at 13:09
Yes this is the solution, THANKS ALLOT! – Bozidar Prcovski Jan 28 at 13:12
@BozidarPrcovski : most welcome friend :) – ρяσѕρєя K Jan 28 at 13:27

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

share|improve this answer
json.getJSONArray(name) -> what value should i put in the name? – Bozidar Prcovski Jan 28 at 12:14
no need for a name. your string is a JSONArray so just initialise the object by passing the string into the constructor of JSONArray – Osama Rao Jan 28 at 12:18
@OsamaRao here he is dealing with url – Andremoniy Jan 28 at 12:19

Take a look here : JSON Tutorial.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.