How to parse JSON Array (Not Json Object) in Android





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







74















I have a trouble finding a way how to parse JSONArray.
It looks like this:



[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]


I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects).
But it's all I have and have to go with it.



*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out.
There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.



Can somebody please give me some hint, or a tutorial or an example?



Much appreciated !










share|improve this question




















  • 1





    Try this to see if your json is valid json.parser.online.fr

    – An-droid
    Sep 24 '13 at 9:02











  • are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.

    – nilkash
    Sep 24 '13 at 9:03











  • It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android

    – SteBra
    Sep 24 '13 at 9:03






  • 1





    @nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray

    – SteBra
    Sep 24 '13 at 9:04











  • Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.

    – Spring Breaker
    Sep 24 '13 at 9:09


















74















I have a trouble finding a way how to parse JSONArray.
It looks like this:



[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]


I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects).
But it's all I have and have to go with it.



*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out.
There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.



Can somebody please give me some hint, or a tutorial or an example?



Much appreciated !










share|improve this question




















  • 1





    Try this to see if your json is valid json.parser.online.fr

    – An-droid
    Sep 24 '13 at 9:02











  • are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.

    – nilkash
    Sep 24 '13 at 9:03











  • It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android

    – SteBra
    Sep 24 '13 at 9:03






  • 1





    @nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray

    – SteBra
    Sep 24 '13 at 9:04











  • Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.

    – Spring Breaker
    Sep 24 '13 at 9:09














74












74








74


22






I have a trouble finding a way how to parse JSONArray.
It looks like this:



[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]


I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects).
But it's all I have and have to go with it.



*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out.
There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.



Can somebody please give me some hint, or a tutorial or an example?



Much appreciated !










share|improve this question
















I have a trouble finding a way how to parse JSONArray.
It looks like this:



[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]


I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects).
But it's all I have and have to go with it.



*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out.
There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.



Can somebody please give me some hint, or a tutorial or an example?



Much appreciated !







java android json gson arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 10 '14 at 10:44







SteBra

















asked Sep 24 '13 at 9:01









SteBraSteBra

2,29242959




2,29242959








  • 1





    Try this to see if your json is valid json.parser.online.fr

    – An-droid
    Sep 24 '13 at 9:02











  • are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.

    – nilkash
    Sep 24 '13 at 9:03











  • It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android

    – SteBra
    Sep 24 '13 at 9:03






  • 1





    @nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray

    – SteBra
    Sep 24 '13 at 9:04











  • Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.

    – Spring Breaker
    Sep 24 '13 at 9:09














  • 1





    Try this to see if your json is valid json.parser.online.fr

    – An-droid
    Sep 24 '13 at 9:02











  • are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.

    – nilkash
    Sep 24 '13 at 9:03











  • It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android

    – SteBra
    Sep 24 '13 at 9:03






  • 1





    @nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray

    – SteBra
    Sep 24 '13 at 9:04











  • Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.

    – Spring Breaker
    Sep 24 '13 at 9:09








1




1





Try this to see if your json is valid json.parser.online.fr

– An-droid
Sep 24 '13 at 9:02





Try this to see if your json is valid json.parser.online.fr

– An-droid
Sep 24 '13 at 9:02













are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.

– nilkash
Sep 24 '13 at 9:03





are you using any library for parsing json? Use gson library for json parsing.Much Helpfull.

– nilkash
Sep 24 '13 at 9:03













It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android

– SteBra
Sep 24 '13 at 9:03





It is valid. I parsed it in an app i did for an iPhone And it's working. I just dont know how to do it in Android

– SteBra
Sep 24 '13 at 9:03




1




1





@nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray

– SteBra
Sep 24 '13 at 9:04





@nikash, yeah i noticed i could use gson. only problem is, all examples i found are for parsing a JSONObject not JSONArray

– SteBra
Sep 24 '13 at 9:04













Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.

– Spring Breaker
Sep 24 '13 at 9:09





Post your code that you have tried so far.It will be helpful to give a precise answer accordingly.

– Spring Breaker
Sep 24 '13 at 9:09












11 Answers
11






active

oldest

votes


















121














use the following snippet to parse the JsonArray.



JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}


Hope it helps.






share|improve this answer


























  • just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???

    – BiggDawgg
    Feb 17 '15 at 19:36











  • @BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.

    – Spring Breaker
    Feb 18 '15 at 5:24



















24














I'll just give a little Jackson example:



First create a data holder which has the fields from JSON string



// imports
// ...
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyDataHolder {
@JsonProperty("name")
public String mName;

@JsonProperty("url")
public String mUrl;
}


And parse list of MyDataHolders



String jsonString = // your json
ObjectMapper mapper = new ObjectMapper();
List<MyDataHolder> list = mapper.readValue(jsonString,
new TypeReference<ArrayList<MyDataHolder>>() {});


Using list items



String firstName = list.get(0).mName;
String secondName = list.get(1).mName;





share|improve this answer


























  • Can you give me an example how would i get JSON as a string from url i have, please ?

    – SteBra
    Sep 24 '13 at 10:37






  • 2





    Weeell here's something that I made up: pastebin.com/w4thyPVK

    – vilpe89
    Sep 24 '13 at 11:24






  • 1





    The DefaultHttpClients execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.

    – vilpe89
    Sep 24 '13 at 11:39






  • 1





    have you imported OnHttpGetCompletedListener? Like this import your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;

    – vilpe89
    Sep 24 '13 at 12:45






  • 1





    Yeap, that was the problem... Thanks for solving my issue :)

    – SteBra
    Sep 24 '13 at 12:59



















19














public static void main(String args) throws JSONException {
String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";

JSONArray jsonarray = new JSONArray(str);


for(int i=0; i<jsonarray.length(); i++){
JSONObject obj = jsonarray.getJSONObject(i);

String name = obj.getString("name");
String url = obj.getString("url");

System.out.println(name);
System.out.println(url);
}
}


Output:



name1
url1
name2
url2





share|improve this answer































    11














    Create a class to hold the objects.



    public class Person{
    private String name;
    private String url;
    //Get & Set methods for each field
    }


    Then deserialize as follows:



    Gson gson = new Gson();
    Person person = gson.fromJson(input, Person.class); //input is your String


    Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/






    share|improve this answer
























    • I asume this "input" would be URL to that json? or is it something else?

      – SteBra
      Sep 24 '13 at 9:09











    • @Stebra, Exactly, I included that as a comment in the code.

      – Kevin Bowersox
      Sep 24 '13 at 9:09











    • This is a very simple solution. It should be the accepted one in my view

      – FabioC
      Nov 18 '17 at 11:38



















    5














    In this example there are several objects inside one json array. That is,



    This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]



    This is one object: {"name":"name1","url":"url1"}



    Assuming that you have got the result to a String variable called jSonResultString:



    JSONArray arr = new JSONArray(jSonResultString);

    //loop through each object
    for (int i=0; i<arr.length(); i++){

    JSONObject jsonProductObject = arr.getJSONObject(i);
    String name = jsonProductObject.getString("name");
    String url = jsonProductObject.getString("url");


    }





    share|improve this answer
























    • did you try this?

      – TharakaNirmana
      Sep 24 '13 at 9:16











    • Im having a hard time getting my JSONstring from URL. Maybe you could help with that?

      – SteBra
      Sep 24 '13 at 10:49











    • Send me the URL

      – TharakaNirmana
      Sep 24 '13 at 11:00



















    3














    @Stebra See this example. This may help you.



    public class CustomerInfo 
    {
    @SerializedName("customerid")
    public String customerid;
    @SerializedName("picture")
    public String picture;

    @SerializedName("location")
    public String location;

    public CustomerInfo()
    {}
    }


    And when you get the result; parse like this



    List<CustomerInfo> customers = null;
    customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());





    share|improve this answer



















    • 1





      Result variable is your JSONArray String

      – nilkash
      Sep 24 '13 at 9:10











    • Would result variable be the url i need to fetch json from or already fetched json ?

      – SteBra
      Sep 24 '13 at 9:22






    • 1





      result string is already fetched JSONArray string

      – nilkash
      Sep 24 '13 at 9:25











    • by far the easiest solution

      – Danilo Salvio
      Sep 8 '16 at 3:35



















    2














    A few great suggestions are already mentioned.
    Using GSON is really handy indeed, and to make life even easier you can try this website
    It's called jsonschema2pojo and does exactly that:



    You give it your json and it generates a java object that can paste in your project.
    You can select GSON to annotate your variables, so extracting the object from your json gets even easier!






    share|improve this answer































      1














      My case
      Load From Server Example..



      int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
      if (jsonLength != 1) {
      for (int i = 0; i < jsonLength; i++) {
      JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
      JSONObject resJson = (JSONObject) jsonArray.get(i);
      //addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
      }


      Hope it help






      share|improve this answer
























      • p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.

        – bong jae choe
        Nov 5 '15 at 5:10





















      -1














      Create a POJO Java Class for the objects in the list like so:



      class NameUrlClass{
      private String name;
      private String url;
      //Constructor
      public NameUrlClass(String name,String url){
      this.name = name;
      this.url = url;
      }
      }


      Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:



      List<NameUrlClass> obj = new ArrayList<NameUrlClass>;


      You can use store the JSON array in this object



      obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]





      share|improve this answer
























      • It's not clear what obj = JSONArray; is supposed to do. What is JSONArray in this context? Where does it come from? What is the purpose of initializing obj with a new ArrayList<NameUrlClass>, when you replace it directly afterwards anyways?

        – Max Vollmer
        Oct 24 '18 at 11:38



















      -3














                  URL url = new URL("your URL");
      connection = (HttpURLConnection) url.openConnection();
      connection.connect();
      InputStream stream = connection.getInputStream();
      BufferedReader reader;
      reader = new BufferedReader(new InputStreamReader(stream));
      StringBuffer buffer = new StringBuffer();
      String line = "";
      while ((line = reader.readLine()) != null) {
      buffer.append(line);
      }

      //setting the json string
      String finalJson = buffer.toString();

      //this is your string get the pattern from buffer.
      JSONArray jsonarray = new JSONArray(finalJson);





      share|improve this answer































        -4














        Old post I know, but unless I've misunderstood the question, this should do the trick:



        s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
        eval("array=" + s);
        for (var i = 0; i < array.length; i++) {
        for (var index in array[i]) {
        alert(array[i][index]);
        }


        }






        share|improve this answer



















        • 2





          Wrong language.

          – Sotirios Delimanolis
          Jun 30 '16 at 17:47












        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18977144%2fhow-to-parse-json-array-not-json-object-in-android%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        11 Answers
        11






        active

        oldest

        votes








        11 Answers
        11






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        121














        use the following snippet to parse the JsonArray.



        JSONArray jsonarray = new JSONArray(jsonStr);
        for (int i = 0; i < jsonarray.length(); i++) {
        JSONObject jsonobject = jsonarray.getJSONObject(i);
        String name = jsonobject.getString("name");
        String url = jsonobject.getString("url");
        }


        Hope it helps.






        share|improve this answer


























        • just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???

          – BiggDawgg
          Feb 17 '15 at 19:36











        • @BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.

          – Spring Breaker
          Feb 18 '15 at 5:24
















        121














        use the following snippet to parse the JsonArray.



        JSONArray jsonarray = new JSONArray(jsonStr);
        for (int i = 0; i < jsonarray.length(); i++) {
        JSONObject jsonobject = jsonarray.getJSONObject(i);
        String name = jsonobject.getString("name");
        String url = jsonobject.getString("url");
        }


        Hope it helps.






        share|improve this answer


























        • just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???

          – BiggDawgg
          Feb 17 '15 at 19:36











        • @BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.

          – Spring Breaker
          Feb 18 '15 at 5:24














        121












        121








        121







        use the following snippet to parse the JsonArray.



        JSONArray jsonarray = new JSONArray(jsonStr);
        for (int i = 0; i < jsonarray.length(); i++) {
        JSONObject jsonobject = jsonarray.getJSONObject(i);
        String name = jsonobject.getString("name");
        String url = jsonobject.getString("url");
        }


        Hope it helps.






        share|improve this answer















        use the following snippet to parse the JsonArray.



        JSONArray jsonarray = new JSONArray(jsonStr);
        for (int i = 0; i < jsonarray.length(); i++) {
        JSONObject jsonobject = jsonarray.getJSONObject(i);
        String name = jsonobject.getString("name");
        String url = jsonobject.getString("url");
        }


        Hope it helps.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 23 '16 at 11:54









        cegprakash

        1,2651639




        1,2651639










        answered Sep 24 '13 at 9:05









        Spring BreakerSpring Breaker

        6,84932455




        6,84932455













        • just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???

          – BiggDawgg
          Feb 17 '15 at 19:36











        • @BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.

          – Spring Breaker
          Feb 18 '15 at 5:24



















        • just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???

          – BiggDawgg
          Feb 17 '15 at 19:36











        • @BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.

          – Spring Breaker
          Feb 18 '15 at 5:24

















        just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???

        – BiggDawgg
        Feb 17 '15 at 19:36





        just one question if jsonarray includes numerics like [2].[4] and that numerics inside includes again an array so if i want to retrieve from especially lets say the array nested in numeric [4] in java code i must write int i=4???

        – BiggDawgg
        Feb 17 '15 at 19:36













        @BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.

        – Spring Breaker
        Feb 18 '15 at 5:24





        @BiggDawgg: Glad it helped you. Post your question in a separate thread with JSON data so that it would be easy for me to answer.

        – Spring Breaker
        Feb 18 '15 at 5:24













        24














        I'll just give a little Jackson example:



        First create a data holder which has the fields from JSON string



        // imports
        // ...
        @JsonIgnoreProperties(ignoreUnknown = true)
        public class MyDataHolder {
        @JsonProperty("name")
        public String mName;

        @JsonProperty("url")
        public String mUrl;
        }


        And parse list of MyDataHolders



        String jsonString = // your json
        ObjectMapper mapper = new ObjectMapper();
        List<MyDataHolder> list = mapper.readValue(jsonString,
        new TypeReference<ArrayList<MyDataHolder>>() {});


        Using list items



        String firstName = list.get(0).mName;
        String secondName = list.get(1).mName;





        share|improve this answer


























        • Can you give me an example how would i get JSON as a string from url i have, please ?

          – SteBra
          Sep 24 '13 at 10:37






        • 2





          Weeell here's something that I made up: pastebin.com/w4thyPVK

          – vilpe89
          Sep 24 '13 at 11:24






        • 1





          The DefaultHttpClients execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.

          – vilpe89
          Sep 24 '13 at 11:39






        • 1





          have you imported OnHttpGetCompletedListener? Like this import your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;

          – vilpe89
          Sep 24 '13 at 12:45






        • 1





          Yeap, that was the problem... Thanks for solving my issue :)

          – SteBra
          Sep 24 '13 at 12:59
















        24














        I'll just give a little Jackson example:



        First create a data holder which has the fields from JSON string



        // imports
        // ...
        @JsonIgnoreProperties(ignoreUnknown = true)
        public class MyDataHolder {
        @JsonProperty("name")
        public String mName;

        @JsonProperty("url")
        public String mUrl;
        }


        And parse list of MyDataHolders



        String jsonString = // your json
        ObjectMapper mapper = new ObjectMapper();
        List<MyDataHolder> list = mapper.readValue(jsonString,
        new TypeReference<ArrayList<MyDataHolder>>() {});


        Using list items



        String firstName = list.get(0).mName;
        String secondName = list.get(1).mName;





        share|improve this answer


























        • Can you give me an example how would i get JSON as a string from url i have, please ?

          – SteBra
          Sep 24 '13 at 10:37






        • 2





          Weeell here's something that I made up: pastebin.com/w4thyPVK

          – vilpe89
          Sep 24 '13 at 11:24






        • 1





          The DefaultHttpClients execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.

          – vilpe89
          Sep 24 '13 at 11:39






        • 1





          have you imported OnHttpGetCompletedListener? Like this import your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;

          – vilpe89
          Sep 24 '13 at 12:45






        • 1





          Yeap, that was the problem... Thanks for solving my issue :)

          – SteBra
          Sep 24 '13 at 12:59














        24












        24








        24







        I'll just give a little Jackson example:



        First create a data holder which has the fields from JSON string



        // imports
        // ...
        @JsonIgnoreProperties(ignoreUnknown = true)
        public class MyDataHolder {
        @JsonProperty("name")
        public String mName;

        @JsonProperty("url")
        public String mUrl;
        }


        And parse list of MyDataHolders



        String jsonString = // your json
        ObjectMapper mapper = new ObjectMapper();
        List<MyDataHolder> list = mapper.readValue(jsonString,
        new TypeReference<ArrayList<MyDataHolder>>() {});


        Using list items



        String firstName = list.get(0).mName;
        String secondName = list.get(1).mName;





        share|improve this answer















        I'll just give a little Jackson example:



        First create a data holder which has the fields from JSON string



        // imports
        // ...
        @JsonIgnoreProperties(ignoreUnknown = true)
        public class MyDataHolder {
        @JsonProperty("name")
        public String mName;

        @JsonProperty("url")
        public String mUrl;
        }


        And parse list of MyDataHolders



        String jsonString = // your json
        ObjectMapper mapper = new ObjectMapper();
        List<MyDataHolder> list = mapper.readValue(jsonString,
        new TypeReference<ArrayList<MyDataHolder>>() {});


        Using list items



        String firstName = list.get(0).mName;
        String secondName = list.get(1).mName;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 24 '13 at 10:07

























        answered Sep 24 '13 at 10:01









        vilpe89vilpe89

        3,70112232




        3,70112232













        • Can you give me an example how would i get JSON as a string from url i have, please ?

          – SteBra
          Sep 24 '13 at 10:37






        • 2





          Weeell here's something that I made up: pastebin.com/w4thyPVK

          – vilpe89
          Sep 24 '13 at 11:24






        • 1





          The DefaultHttpClients execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.

          – vilpe89
          Sep 24 '13 at 11:39






        • 1





          have you imported OnHttpGetCompletedListener? Like this import your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;

          – vilpe89
          Sep 24 '13 at 12:45






        • 1





          Yeap, that was the problem... Thanks for solving my issue :)

          – SteBra
          Sep 24 '13 at 12:59



















        • Can you give me an example how would i get JSON as a string from url i have, please ?

          – SteBra
          Sep 24 '13 at 10:37






        • 2





          Weeell here's something that I made up: pastebin.com/w4thyPVK

          – vilpe89
          Sep 24 '13 at 11:24






        • 1





          The DefaultHttpClients execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.

          – vilpe89
          Sep 24 '13 at 11:39






        • 1





          have you imported OnHttpGetCompletedListener? Like this import your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;

          – vilpe89
          Sep 24 '13 at 12:45






        • 1





          Yeap, that was the problem... Thanks for solving my issue :)

          – SteBra
          Sep 24 '13 at 12:59

















        Can you give me an example how would i get JSON as a string from url i have, please ?

        – SteBra
        Sep 24 '13 at 10:37





        Can you give me an example how would i get JSON as a string from url i have, please ?

        – SteBra
        Sep 24 '13 at 10:37




        2




        2





        Weeell here's something that I made up: pastebin.com/w4thyPVK

        – vilpe89
        Sep 24 '13 at 11:24





        Weeell here's something that I made up: pastebin.com/w4thyPVK

        – vilpe89
        Sep 24 '13 at 11:24




        1




        1





        The DefaultHttpClients execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.

        – vilpe89
        Sep 24 '13 at 11:39





        The DefaultHttpClients execute-method is a blocking call so it has to be done in a background task, just to make clear why it is in separate class.

        – vilpe89
        Sep 24 '13 at 11:39




        1




        1





        have you imported OnHttpGetCompletedListener? Like this import your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;

        – vilpe89
        Sep 24 '13 at 12:45





        have you imported OnHttpGetCompletedListener? Like this import your_package_name_here.HttpGetTask.OnHttpGetCompletedListener;

        – vilpe89
        Sep 24 '13 at 12:45




        1




        1





        Yeap, that was the problem... Thanks for solving my issue :)

        – SteBra
        Sep 24 '13 at 12:59





        Yeap, that was the problem... Thanks for solving my issue :)

        – SteBra
        Sep 24 '13 at 12:59











        19














        public static void main(String args) throws JSONException {
        String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";

        JSONArray jsonarray = new JSONArray(str);


        for(int i=0; i<jsonarray.length(); i++){
        JSONObject obj = jsonarray.getJSONObject(i);

        String name = obj.getString("name");
        String url = obj.getString("url");

        System.out.println(name);
        System.out.println(url);
        }
        }


        Output:



        name1
        url1
        name2
        url2





        share|improve this answer




























          19














          public static void main(String args) throws JSONException {
          String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";

          JSONArray jsonarray = new JSONArray(str);


          for(int i=0; i<jsonarray.length(); i++){
          JSONObject obj = jsonarray.getJSONObject(i);

          String name = obj.getString("name");
          String url = obj.getString("url");

          System.out.println(name);
          System.out.println(url);
          }
          }


          Output:



          name1
          url1
          name2
          url2





          share|improve this answer


























            19












            19








            19







            public static void main(String args) throws JSONException {
            String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";

            JSONArray jsonarray = new JSONArray(str);


            for(int i=0; i<jsonarray.length(); i++){
            JSONObject obj = jsonarray.getJSONObject(i);

            String name = obj.getString("name");
            String url = obj.getString("url");

            System.out.println(name);
            System.out.println(url);
            }
            }


            Output:



            name1
            url1
            name2
            url2





            share|improve this answer













            public static void main(String args) throws JSONException {
            String str = "[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]";

            JSONArray jsonarray = new JSONArray(str);


            for(int i=0; i<jsonarray.length(); i++){
            JSONObject obj = jsonarray.getJSONObject(i);

            String name = obj.getString("name");
            String url = obj.getString("url");

            System.out.println(name);
            System.out.println(url);
            }
            }


            Output:



            name1
            url1
            name2
            url2






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 24 '13 at 9:12









            Maxim ShoustinMaxim Shoustin

            68.3k25181210




            68.3k25181210























                11














                Create a class to hold the objects.



                public class Person{
                private String name;
                private String url;
                //Get & Set methods for each field
                }


                Then deserialize as follows:



                Gson gson = new Gson();
                Person person = gson.fromJson(input, Person.class); //input is your String


                Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/






                share|improve this answer
























                • I asume this "input" would be URL to that json? or is it something else?

                  – SteBra
                  Sep 24 '13 at 9:09











                • @Stebra, Exactly, I included that as a comment in the code.

                  – Kevin Bowersox
                  Sep 24 '13 at 9:09











                • This is a very simple solution. It should be the accepted one in my view

                  – FabioC
                  Nov 18 '17 at 11:38
















                11














                Create a class to hold the objects.



                public class Person{
                private String name;
                private String url;
                //Get & Set methods for each field
                }


                Then deserialize as follows:



                Gson gson = new Gson();
                Person person = gson.fromJson(input, Person.class); //input is your String


                Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/






                share|improve this answer
























                • I asume this "input" would be URL to that json? or is it something else?

                  – SteBra
                  Sep 24 '13 at 9:09











                • @Stebra, Exactly, I included that as a comment in the code.

                  – Kevin Bowersox
                  Sep 24 '13 at 9:09











                • This is a very simple solution. It should be the accepted one in my view

                  – FabioC
                  Nov 18 '17 at 11:38














                11












                11








                11







                Create a class to hold the objects.



                public class Person{
                private String name;
                private String url;
                //Get & Set methods for each field
                }


                Then deserialize as follows:



                Gson gson = new Gson();
                Person person = gson.fromJson(input, Person.class); //input is your String


                Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/






                share|improve this answer













                Create a class to hold the objects.



                public class Person{
                private String name;
                private String url;
                //Get & Set methods for each field
                }


                Then deserialize as follows:



                Gson gson = new Gson();
                Person person = gson.fromJson(input, Person.class); //input is your String


                Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 24 '13 at 9:08









                Kevin BowersoxKevin Bowersox

                79.5k14124166




                79.5k14124166













                • I asume this "input" would be URL to that json? or is it something else?

                  – SteBra
                  Sep 24 '13 at 9:09











                • @Stebra, Exactly, I included that as a comment in the code.

                  – Kevin Bowersox
                  Sep 24 '13 at 9:09











                • This is a very simple solution. It should be the accepted one in my view

                  – FabioC
                  Nov 18 '17 at 11:38



















                • I asume this "input" would be URL to that json? or is it something else?

                  – SteBra
                  Sep 24 '13 at 9:09











                • @Stebra, Exactly, I included that as a comment in the code.

                  – Kevin Bowersox
                  Sep 24 '13 at 9:09











                • This is a very simple solution. It should be the accepted one in my view

                  – FabioC
                  Nov 18 '17 at 11:38

















                I asume this "input" would be URL to that json? or is it something else?

                – SteBra
                Sep 24 '13 at 9:09





                I asume this "input" would be URL to that json? or is it something else?

                – SteBra
                Sep 24 '13 at 9:09













                @Stebra, Exactly, I included that as a comment in the code.

                – Kevin Bowersox
                Sep 24 '13 at 9:09





                @Stebra, Exactly, I included that as a comment in the code.

                – Kevin Bowersox
                Sep 24 '13 at 9:09













                This is a very simple solution. It should be the accepted one in my view

                – FabioC
                Nov 18 '17 at 11:38





                This is a very simple solution. It should be the accepted one in my view

                – FabioC
                Nov 18 '17 at 11:38











                5














                In this example there are several objects inside one json array. That is,



                This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]



                This is one object: {"name":"name1","url":"url1"}



                Assuming that you have got the result to a String variable called jSonResultString:



                JSONArray arr = new JSONArray(jSonResultString);

                //loop through each object
                for (int i=0; i<arr.length(); i++){

                JSONObject jsonProductObject = arr.getJSONObject(i);
                String name = jsonProductObject.getString("name");
                String url = jsonProductObject.getString("url");


                }





                share|improve this answer
























                • did you try this?

                  – TharakaNirmana
                  Sep 24 '13 at 9:16











                • Im having a hard time getting my JSONstring from URL. Maybe you could help with that?

                  – SteBra
                  Sep 24 '13 at 10:49











                • Send me the URL

                  – TharakaNirmana
                  Sep 24 '13 at 11:00
















                5














                In this example there are several objects inside one json array. That is,



                This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]



                This is one object: {"name":"name1","url":"url1"}



                Assuming that you have got the result to a String variable called jSonResultString:



                JSONArray arr = new JSONArray(jSonResultString);

                //loop through each object
                for (int i=0; i<arr.length(); i++){

                JSONObject jsonProductObject = arr.getJSONObject(i);
                String name = jsonProductObject.getString("name");
                String url = jsonProductObject.getString("url");


                }





                share|improve this answer
























                • did you try this?

                  – TharakaNirmana
                  Sep 24 '13 at 9:16











                • Im having a hard time getting my JSONstring from URL. Maybe you could help with that?

                  – SteBra
                  Sep 24 '13 at 10:49











                • Send me the URL

                  – TharakaNirmana
                  Sep 24 '13 at 11:00














                5












                5








                5







                In this example there are several objects inside one json array. That is,



                This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]



                This is one object: {"name":"name1","url":"url1"}



                Assuming that you have got the result to a String variable called jSonResultString:



                JSONArray arr = new JSONArray(jSonResultString);

                //loop through each object
                for (int i=0; i<arr.length(); i++){

                JSONObject jsonProductObject = arr.getJSONObject(i);
                String name = jsonProductObject.getString("name");
                String url = jsonProductObject.getString("url");


                }





                share|improve this answer













                In this example there are several objects inside one json array. That is,



                This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]



                This is one object: {"name":"name1","url":"url1"}



                Assuming that you have got the result to a String variable called jSonResultString:



                JSONArray arr = new JSONArray(jSonResultString);

                //loop through each object
                for (int i=0; i<arr.length(); i++){

                JSONObject jsonProductObject = arr.getJSONObject(i);
                String name = jsonProductObject.getString("name");
                String url = jsonProductObject.getString("url");


                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 24 '13 at 9:09









                TharakaNirmanaTharakaNirmana

                7,65973966




                7,65973966













                • did you try this?

                  – TharakaNirmana
                  Sep 24 '13 at 9:16











                • Im having a hard time getting my JSONstring from URL. Maybe you could help with that?

                  – SteBra
                  Sep 24 '13 at 10:49











                • Send me the URL

                  – TharakaNirmana
                  Sep 24 '13 at 11:00



















                • did you try this?

                  – TharakaNirmana
                  Sep 24 '13 at 9:16











                • Im having a hard time getting my JSONstring from URL. Maybe you could help with that?

                  – SteBra
                  Sep 24 '13 at 10:49











                • Send me the URL

                  – TharakaNirmana
                  Sep 24 '13 at 11:00

















                did you try this?

                – TharakaNirmana
                Sep 24 '13 at 9:16





                did you try this?

                – TharakaNirmana
                Sep 24 '13 at 9:16













                Im having a hard time getting my JSONstring from URL. Maybe you could help with that?

                – SteBra
                Sep 24 '13 at 10:49





                Im having a hard time getting my JSONstring from URL. Maybe you could help with that?

                – SteBra
                Sep 24 '13 at 10:49













                Send me the URL

                – TharakaNirmana
                Sep 24 '13 at 11:00





                Send me the URL

                – TharakaNirmana
                Sep 24 '13 at 11:00











                3














                @Stebra See this example. This may help you.



                public class CustomerInfo 
                {
                @SerializedName("customerid")
                public String customerid;
                @SerializedName("picture")
                public String picture;

                @SerializedName("location")
                public String location;

                public CustomerInfo()
                {}
                }


                And when you get the result; parse like this



                List<CustomerInfo> customers = null;
                customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());





                share|improve this answer



















                • 1





                  Result variable is your JSONArray String

                  – nilkash
                  Sep 24 '13 at 9:10











                • Would result variable be the url i need to fetch json from or already fetched json ?

                  – SteBra
                  Sep 24 '13 at 9:22






                • 1





                  result string is already fetched JSONArray string

                  – nilkash
                  Sep 24 '13 at 9:25











                • by far the easiest solution

                  – Danilo Salvio
                  Sep 8 '16 at 3:35
















                3














                @Stebra See this example. This may help you.



                public class CustomerInfo 
                {
                @SerializedName("customerid")
                public String customerid;
                @SerializedName("picture")
                public String picture;

                @SerializedName("location")
                public String location;

                public CustomerInfo()
                {}
                }


                And when you get the result; parse like this



                List<CustomerInfo> customers = null;
                customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());





                share|improve this answer



















                • 1





                  Result variable is your JSONArray String

                  – nilkash
                  Sep 24 '13 at 9:10











                • Would result variable be the url i need to fetch json from or already fetched json ?

                  – SteBra
                  Sep 24 '13 at 9:22






                • 1





                  result string is already fetched JSONArray string

                  – nilkash
                  Sep 24 '13 at 9:25











                • by far the easiest solution

                  – Danilo Salvio
                  Sep 8 '16 at 3:35














                3












                3








                3







                @Stebra See this example. This may help you.



                public class CustomerInfo 
                {
                @SerializedName("customerid")
                public String customerid;
                @SerializedName("picture")
                public String picture;

                @SerializedName("location")
                public String location;

                public CustomerInfo()
                {}
                }


                And when you get the result; parse like this



                List<CustomerInfo> customers = null;
                customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());





                share|improve this answer













                @Stebra See this example. This may help you.



                public class CustomerInfo 
                {
                @SerializedName("customerid")
                public String customerid;
                @SerializedName("picture")
                public String picture;

                @SerializedName("location")
                public String location;

                public CustomerInfo()
                {}
                }


                And when you get the result; parse like this



                List<CustomerInfo> customers = null;
                customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 24 '13 at 9:09









                nilkashnilkash

                3,2462382151




                3,2462382151








                • 1





                  Result variable is your JSONArray String

                  – nilkash
                  Sep 24 '13 at 9:10











                • Would result variable be the url i need to fetch json from or already fetched json ?

                  – SteBra
                  Sep 24 '13 at 9:22






                • 1





                  result string is already fetched JSONArray string

                  – nilkash
                  Sep 24 '13 at 9:25











                • by far the easiest solution

                  – Danilo Salvio
                  Sep 8 '16 at 3:35














                • 1





                  Result variable is your JSONArray String

                  – nilkash
                  Sep 24 '13 at 9:10











                • Would result variable be the url i need to fetch json from or already fetched json ?

                  – SteBra
                  Sep 24 '13 at 9:22






                • 1





                  result string is already fetched JSONArray string

                  – nilkash
                  Sep 24 '13 at 9:25











                • by far the easiest solution

                  – Danilo Salvio
                  Sep 8 '16 at 3:35








                1




                1





                Result variable is your JSONArray String

                – nilkash
                Sep 24 '13 at 9:10





                Result variable is your JSONArray String

                – nilkash
                Sep 24 '13 at 9:10













                Would result variable be the url i need to fetch json from or already fetched json ?

                – SteBra
                Sep 24 '13 at 9:22





                Would result variable be the url i need to fetch json from or already fetched json ?

                – SteBra
                Sep 24 '13 at 9:22




                1




                1





                result string is already fetched JSONArray string

                – nilkash
                Sep 24 '13 at 9:25





                result string is already fetched JSONArray string

                – nilkash
                Sep 24 '13 at 9:25













                by far the easiest solution

                – Danilo Salvio
                Sep 8 '16 at 3:35





                by far the easiest solution

                – Danilo Salvio
                Sep 8 '16 at 3:35











                2














                A few great suggestions are already mentioned.
                Using GSON is really handy indeed, and to make life even easier you can try this website
                It's called jsonschema2pojo and does exactly that:



                You give it your json and it generates a java object that can paste in your project.
                You can select GSON to annotate your variables, so extracting the object from your json gets even easier!






                share|improve this answer




























                  2














                  A few great suggestions are already mentioned.
                  Using GSON is really handy indeed, and to make life even easier you can try this website
                  It's called jsonschema2pojo and does exactly that:



                  You give it your json and it generates a java object that can paste in your project.
                  You can select GSON to annotate your variables, so extracting the object from your json gets even easier!






                  share|improve this answer


























                    2












                    2








                    2







                    A few great suggestions are already mentioned.
                    Using GSON is really handy indeed, and to make life even easier you can try this website
                    It's called jsonschema2pojo and does exactly that:



                    You give it your json and it generates a java object that can paste in your project.
                    You can select GSON to annotate your variables, so extracting the object from your json gets even easier!






                    share|improve this answer













                    A few great suggestions are already mentioned.
                    Using GSON is really handy indeed, and to make life even easier you can try this website
                    It's called jsonschema2pojo and does exactly that:



                    You give it your json and it generates a java object that can paste in your project.
                    You can select GSON to annotate your variables, so extracting the object from your json gets even easier!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 3 '14 at 15:56









                    TomCBTomCB

                    1,82262654




                    1,82262654























                        1














                        My case
                        Load From Server Example..



                        int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
                        if (jsonLength != 1) {
                        for (int i = 0; i < jsonLength; i++) {
                        JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
                        JSONObject resJson = (JSONObject) jsonArray.get(i);
                        //addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
                        }


                        Hope it help






                        share|improve this answer
























                        • p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.

                          – bong jae choe
                          Nov 5 '15 at 5:10


















                        1














                        My case
                        Load From Server Example..



                        int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
                        if (jsonLength != 1) {
                        for (int i = 0; i < jsonLength; i++) {
                        JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
                        JSONObject resJson = (JSONObject) jsonArray.get(i);
                        //addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
                        }


                        Hope it help






                        share|improve this answer
























                        • p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.

                          – bong jae choe
                          Nov 5 '15 at 5:10
















                        1












                        1








                        1







                        My case
                        Load From Server Example..



                        int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
                        if (jsonLength != 1) {
                        for (int i = 0; i < jsonLength; i++) {
                        JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
                        JSONObject resJson = (JSONObject) jsonArray.get(i);
                        //addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
                        }


                        Hope it help






                        share|improve this answer













                        My case
                        Load From Server Example..



                        int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
                        if (jsonLength != 1) {
                        for (int i = 0; i < jsonLength; i++) {
                        JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
                        JSONObject resJson = (JSONObject) jsonArray.get(i);
                        //addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
                        }


                        Hope it help







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 4 '15 at 7:56









                        bong jae choebong jae choe

                        1,3921918




                        1,3921918













                        • p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.

                          – bong jae choe
                          Nov 5 '15 at 5:10





















                        • p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.

                          – bong jae choe
                          Nov 5 '15 at 5:10



















                        p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.

                        – bong jae choe
                        Nov 5 '15 at 5:10







                        p.s "jsonObject.getString("messages")" type is jsonArray; first exist answer is cannot be convert string to jsonArray; i'm fix it.

                        – bong jae choe
                        Nov 5 '15 at 5:10













                        -1














                        Create a POJO Java Class for the objects in the list like so:



                        class NameUrlClass{
                        private String name;
                        private String url;
                        //Constructor
                        public NameUrlClass(String name,String url){
                        this.name = name;
                        this.url = url;
                        }
                        }


                        Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:



                        List<NameUrlClass> obj = new ArrayList<NameUrlClass>;


                        You can use store the JSON array in this object



                        obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]





                        share|improve this answer
























                        • It's not clear what obj = JSONArray; is supposed to do. What is JSONArray in this context? Where does it come from? What is the purpose of initializing obj with a new ArrayList<NameUrlClass>, when you replace it directly afterwards anyways?

                          – Max Vollmer
                          Oct 24 '18 at 11:38
















                        -1














                        Create a POJO Java Class for the objects in the list like so:



                        class NameUrlClass{
                        private String name;
                        private String url;
                        //Constructor
                        public NameUrlClass(String name,String url){
                        this.name = name;
                        this.url = url;
                        }
                        }


                        Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:



                        List<NameUrlClass> obj = new ArrayList<NameUrlClass>;


                        You can use store the JSON array in this object



                        obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]





                        share|improve this answer
























                        • It's not clear what obj = JSONArray; is supposed to do. What is JSONArray in this context? Where does it come from? What is the purpose of initializing obj with a new ArrayList<NameUrlClass>, when you replace it directly afterwards anyways?

                          – Max Vollmer
                          Oct 24 '18 at 11:38














                        -1












                        -1








                        -1







                        Create a POJO Java Class for the objects in the list like so:



                        class NameUrlClass{
                        private String name;
                        private String url;
                        //Constructor
                        public NameUrlClass(String name,String url){
                        this.name = name;
                        this.url = url;
                        }
                        }


                        Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:



                        List<NameUrlClass> obj = new ArrayList<NameUrlClass>;


                        You can use store the JSON array in this object



                        obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]





                        share|improve this answer













                        Create a POJO Java Class for the objects in the list like so:



                        class NameUrlClass{
                        private String name;
                        private String url;
                        //Constructor
                        public NameUrlClass(String name,String url){
                        this.name = name;
                        this.url = url;
                        }
                        }


                        Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:



                        List<NameUrlClass> obj = new ArrayList<NameUrlClass>;


                        You can use store the JSON array in this object



                        obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 24 '18 at 11:17









                        Devesh PradhanDevesh Pradhan

                        11




                        11













                        • It's not clear what obj = JSONArray; is supposed to do. What is JSONArray in this context? Where does it come from? What is the purpose of initializing obj with a new ArrayList<NameUrlClass>, when you replace it directly afterwards anyways?

                          – Max Vollmer
                          Oct 24 '18 at 11:38



















                        • It's not clear what obj = JSONArray; is supposed to do. What is JSONArray in this context? Where does it come from? What is the purpose of initializing obj with a new ArrayList<NameUrlClass>, when you replace it directly afterwards anyways?

                          – Max Vollmer
                          Oct 24 '18 at 11:38

















                        It's not clear what obj = JSONArray; is supposed to do. What is JSONArray in this context? Where does it come from? What is the purpose of initializing obj with a new ArrayList<NameUrlClass>, when you replace it directly afterwards anyways?

                        – Max Vollmer
                        Oct 24 '18 at 11:38





                        It's not clear what obj = JSONArray; is supposed to do. What is JSONArray in this context? Where does it come from? What is the purpose of initializing obj with a new ArrayList<NameUrlClass>, when you replace it directly afterwards anyways?

                        – Max Vollmer
                        Oct 24 '18 at 11:38











                        -3














                                    URL url = new URL("your URL");
                        connection = (HttpURLConnection) url.openConnection();
                        connection.connect();
                        InputStream stream = connection.getInputStream();
                        BufferedReader reader;
                        reader = new BufferedReader(new InputStreamReader(stream));
                        StringBuffer buffer = new StringBuffer();
                        String line = "";
                        while ((line = reader.readLine()) != null) {
                        buffer.append(line);
                        }

                        //setting the json string
                        String finalJson = buffer.toString();

                        //this is your string get the pattern from buffer.
                        JSONArray jsonarray = new JSONArray(finalJson);





                        share|improve this answer




























                          -3














                                      URL url = new URL("your URL");
                          connection = (HttpURLConnection) url.openConnection();
                          connection.connect();
                          InputStream stream = connection.getInputStream();
                          BufferedReader reader;
                          reader = new BufferedReader(new InputStreamReader(stream));
                          StringBuffer buffer = new StringBuffer();
                          String line = "";
                          while ((line = reader.readLine()) != null) {
                          buffer.append(line);
                          }

                          //setting the json string
                          String finalJson = buffer.toString();

                          //this is your string get the pattern from buffer.
                          JSONArray jsonarray = new JSONArray(finalJson);





                          share|improve this answer


























                            -3












                            -3








                            -3







                                        URL url = new URL("your URL");
                            connection = (HttpURLConnection) url.openConnection();
                            connection.connect();
                            InputStream stream = connection.getInputStream();
                            BufferedReader reader;
                            reader = new BufferedReader(new InputStreamReader(stream));
                            StringBuffer buffer = new StringBuffer();
                            String line = "";
                            while ((line = reader.readLine()) != null) {
                            buffer.append(line);
                            }

                            //setting the json string
                            String finalJson = buffer.toString();

                            //this is your string get the pattern from buffer.
                            JSONArray jsonarray = new JSONArray(finalJson);





                            share|improve this answer













                                        URL url = new URL("your URL");
                            connection = (HttpURLConnection) url.openConnection();
                            connection.connect();
                            InputStream stream = connection.getInputStream();
                            BufferedReader reader;
                            reader = new BufferedReader(new InputStreamReader(stream));
                            StringBuffer buffer = new StringBuffer();
                            String line = "";
                            while ((line = reader.readLine()) != null) {
                            buffer.append(line);
                            }

                            //setting the json string
                            String finalJson = buffer.toString();

                            //this is your string get the pattern from buffer.
                            JSONArray jsonarray = new JSONArray(finalJson);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 30 '16 at 14:31









                            boyke purnomoboyke purnomo

                            71




                            71























                                -4














                                Old post I know, but unless I've misunderstood the question, this should do the trick:



                                s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
                                eval("array=" + s);
                                for (var i = 0; i < array.length; i++) {
                                for (var index in array[i]) {
                                alert(array[i][index]);
                                }


                                }






                                share|improve this answer



















                                • 2





                                  Wrong language.

                                  – Sotirios Delimanolis
                                  Jun 30 '16 at 17:47
















                                -4














                                Old post I know, but unless I've misunderstood the question, this should do the trick:



                                s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
                                eval("array=" + s);
                                for (var i = 0; i < array.length; i++) {
                                for (var index in array[i]) {
                                alert(array[i][index]);
                                }


                                }






                                share|improve this answer



















                                • 2





                                  Wrong language.

                                  – Sotirios Delimanolis
                                  Jun 30 '16 at 17:47














                                -4












                                -4








                                -4







                                Old post I know, but unless I've misunderstood the question, this should do the trick:



                                s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
                                eval("array=" + s);
                                for (var i = 0; i < array.length; i++) {
                                for (var index in array[i]) {
                                alert(array[i][index]);
                                }


                                }






                                share|improve this answer













                                Old post I know, but unless I've misunderstood the question, this should do the trick:



                                s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
                                eval("array=" + s);
                                for (var i = 0; i < array.length; i++) {
                                for (var index in array[i]) {
                                alert(array[i][index]);
                                }


                                }







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 9 '15 at 10:34









                                JanusJanus

                                1




                                1








                                • 2





                                  Wrong language.

                                  – Sotirios Delimanolis
                                  Jun 30 '16 at 17:47














                                • 2





                                  Wrong language.

                                  – Sotirios Delimanolis
                                  Jun 30 '16 at 17:47








                                2




                                2





                                Wrong language.

                                – Sotirios Delimanolis
                                Jun 30 '16 at 17:47





                                Wrong language.

                                – Sotirios Delimanolis
                                Jun 30 '16 at 17:47


















                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18977144%2fhow-to-parse-json-array-not-json-object-in-android%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to change which sound is reproduced for terminal bell?

                                Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents