Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:23:58+00:00 2026-06-12T21:23:58+00:00

How do I parse a JSON file like the one below containing school names

  • 0

How do I parse a JSON file like the one below containing school names and IDs and use the values in the “Name” field to populate the spinner lables?

I want the spinner to output the selected SchoolID to the next activity. E.g. So you see “Hill View Elementary” and if you select this the value “HVE” is output to the next activity to act on.

{
"schools": [
{
"Name": "Hill View Elementary",
"SchoolID": "HVE"},
{
"Name": "Mill View",
"SchoolID": "MVE"},
{
"Name": "Big School",
"SchoolID": "BSC"},
]
}

Here is the onCreate I’m using to attempt to read the feed…

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);  

    String readFeed = readFeed();
    try {
      JSONArray jsonArray = new
      JSONArray(readFeed);
      Log.i(MainActivity.class.getName(),
          "Number of entries " + jsonArray.length());

      for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        // Here instead of Logging I want to use each schools "Name" to 
        // be put into an array that I can use to populate the strings 
        // for the spinner

        Log.i(MainActivity.class.getName(), jsonObject.getString("schools"));

      }
    } catch (Exception e) {
      e.printStackTrace();
    }
}

I’m still coming up to speed on Android so clarity appreciated.

Here is readfeed:

public String readFeed() {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();

    // domain intentionally obfuscated for security reasons
    HttpGet httpGet = new HttpGet("http://www.domain.com/schools.php");
    try 
 {
      HttpResponse response = client.execute(httpGet);
      StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      if (statusCode == 200) {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        String
 line;
        while ((line = reader.readLine()) != null) {
          builder.append(line);
        }
      } else {
        Log.e(MainActivity.class.toString(), "Failed to download file");
      }
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e)
 {
      e.printStackTrace();
    }
    return builder.toString();
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:24:00+00:00Added an answer on June 12, 2026 at 9:24 pm

    I would do something like this:

    public class School {
        private String name;
        private String id;
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
    }
    

    On create:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
    
        String readFeed = readFeed();
    
        // you can use this array to find the school ID based on name
        ArrayList<School> schools = new ArrayList<School>();
        // you can use this array to populate your spinner
        ArrayList<String> schoolNames = new ArrayList<String>();
    
        try {
          JSONObject json = new JSONObject(readFeed);
          JSONArray jsonArray = new JSONArray(json.optString("schools"));
          Log.i(MainActivity.class.getName(),
              "Number of entries " + jsonArray.length());
    
          for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
    
            School school = new School();
            school.setName(jsonObject.optString("Name"));
            school.setId(jsonObject.optString("SchoolID"));
            schools.add(school);
            schoolNames.add(jsonObject.optString("Name"));
    
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        Spinner mySpinner = (Spinner)findViewById(R.id.my_spinner);
        mySpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, schoolNames));
    }
    

    In your main_activity.xml you need a Spinner. The code above would work with a Spinner named like below:

    <Spinner
        android:id="@+id/my_spinner"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have parse json file which contain more than hundred location name, there latitude
I need to parse data from one JSON file in order to validate arrays,
I am trying to parse a Json file like this, generated by Exiftool: [{
I'm trying to parse some huge JSON file (like http://eu.battle.net/auction-data/258993a3c6b974ef3e6f22ea6f822720/auctions.json ) using gson library
I am using the Javascript serializer to parse a JSON file. Its runs fine
After I press a button, Android parse a JSON file and pick the info
I have a huge JSON file to parse in my Android app. I suppose
I have a JSON file and want to parse value of variable second value
I am trying to parse a rather large json file, and I am beginning
I'm creating an android application which should parse a Json from a file or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.