-4

I want to parse JSON Array and display result in listview. I have already ask in this community but didn't get helpful answer. Please give me code for this JSON.

JSON

[{
    "city_id": "1",
    "city_name": "Noida"
},
{
    "city_id": "2",
    "city_name": "Delhi"
},
{
    "city_id": "3",
    "city_name": "Gaziyabad"
},
{
    "city_id": "4",
    "city_name": "Gurgaon"
},
{
    "city_id": "5",
    "city_name": "Gr. Noida"
}]

URL

http://14.140.200.186/Hospital/newget_city.php

please help

9
  • what is your listview looklike? Commented Feb 3, 2016 at 10:38
  • 6
    Possible duplicate of Json Parsing in Android Application Commented Feb 3, 2016 at 10:38
  • The question is really broad. You should address one issue at a time. Are you facing issues in Fetching the JSON, parsing the JSON or populating the ListView? Start with one, learn how to do it, and if you face issues, post your question here, with the code that doesn't work at your end. Commented Feb 3, 2016 at 10:41
  • @Phan Văn Linh nothing is going in listview Commented Feb 3, 2016 at 10:48
  • @Shamas i want to simply show cities in listview from the url when i trying this nothig is going in listview i have already post the locat and code click on the link you can see:- stackoverflow.com/questions/35170183/… Commented Feb 3, 2016 at 10:50

2 Answers 2

0

As mentioned the question is too broad, just to give the approach I would take.

  1. Build the model class in this case: City
  2. I would advise using retrofit(http://square.github.io/retrofit/) for the network call, so build the interface
  3. Make the network call
  4. Add the retrieved results in a recyclerview adapter
Sign up to request clarification or add additional context in comments.

Comments

0

Using Gson can do the json parsing job for you. Its easy to integrate and handy to parse json data. You simply need to create a class containing city_id and city_name.

City.java

public class City {

    private String city_id;
    private String city_name;

    public City() {

    }

    public String getCityId() {
        return city_id;
    }

    public String getCityName() {
        return city_name;
    }
}

Now add another class. E.g. CityList.java

import java.util.List;

public class CityList {

    private List<City> cityList;

    public CityList() {
    }

    public List<City> getCityList() {
        return cityList;
    }

}

Now from json string, parse the data into the CityList class.

Gson gson = new Gson();
CityList myCityList = gson.fromJson(jsonString, CityList.class);

Add the gradle dependency for Gson in build.gradle

compile 'com.google.code.gson:gson:2.3'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.