1

this is my code below how do i acess this json file check my method it is coreect method to aces json file array and nodes? i want to acess this json file below from my code help me how do i acess this json file formmy code? i dontknow howmany array in json file

{
    "status":1,
    "message":"",
    "data":
    {
        "school":
        [
        {
            "id":3,
            "name":"FG Public School"
        },
        {
            "id":4,
            "name":"Fazaia Inter College"}
        ]
    }
}




HttpClient client = new DefaultHttpClient();

HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);

HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
HttpUriRequest request = new HttpGet(SelectMenuAPI);
HttpResponse response = client.execute(request);
InputStream atomInputStream = 
response.getEntity().getContent();
BufferedReader in = new BufferedReader(new 
InputStreamReader(atomInputStream));

String line;
String str = "";
while ((line = in.readLine()) != null){
    str += line;
}


JSONObject json = new JSONObject(str);
JSONArray data = json.getJSONArray("school");

for (int i = 0; i < data.length(); i++) {
    JSONObject object =  
    data.getJSONObject(i); 

    //    JSONObject category = 
    object.getJSONObject("Category");


    Category_ID.add(Long.parseLong(object.getString("id")));

    Category_name.add(object.getString("name"));

    Log.d("Category name", 
    Category_name.get(i));
4
  • is not display this code checkmy json code please "school" is aray or node???? Commented Aug 5, 2013 at 9:10
  • Your code has syntax errors. Please make sure you posted the entire loop. Commented Aug 5, 2013 at 9:10
  • school is an array.. every element of the array is a node Commented Aug 5, 2013 at 9:11
  • @user2644835 first fetch the data object value then, fetch the school array values. Commented Aug 5, 2013 at 9:20

3 Answers 3

1
Try This Code
=================================================================
    try {
            String[] Id,name;
          JSONObject json = new JSONObject(str);

          JSONObject SubString3 = json.getJSONObject("data");
          Log.e(SubString3.toString(),"SubString3");
          JSONArray Array = SubString3.getJSONArray("school");
          Id = new String[Array.length()];
          name =new String[Array.length()]; 
          for(int i=0;i<=Array.length();i++)
          {
              Id[i]= Array.getJSONObject(i).getString("id");
              Log.e(Id[i].toString(),"Id[i]");
              name[i]= Array.getJSONObject(i).getString("name");
              Log.e(name[i].toString(),"name[i]");
          }
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
Sign up to request clarification or add additional context in comments.

4 Comments

where u used string 1 and string 2??
Substring1 And Substring2 store just String Of Status and Message Its Just Parsing.
See i Update My Answer I hope You got your Answer.
not working answer here plz i paste my code stackoverflow.com/questions/18055689/…
1

The Category object is not present inside the Json file

you can try following

    JSONObject json = new JSONObject("str");
    JSONObject data = json.getJSONObject("data");
    JSONArray school = json.getJSONArray("school");

    for (int i = 0; i < data.length(); i++) {
        JSONObject object = school.getJSONObject(i);

        long id = Long.parseLong(object.getString("id"));
        String name = object.getString("name");
        JSONObject category = new JSONObject();
                    category.put("id");
                    category.put("name");




    }

1 Comment

i wantto add incategory object then load in listview
0

First you need to fetch an OBJECT of the whole JSON because it's in '{' '}'. Then you need to fetch an object again with the name "data" because it is in '{' '}'. And only then should you fetch the array named "school".

JSONObject json = new JSONObject(str);
JSONObject data = json.getJSONObject("data");
JSONArray school = data.getJSONArray("school");

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.