0

Here is my Json String. I want To extract Country Name and Country Id of each Object.. please Help Me for the Same

String===

[{"CountryID":1,"CountryIDNew":1,"Name":"India","Shname":"IN","Des":null,"InternationalCode":null,"ISDCode":"091","Active":true,"SoftDelete":false,"CountryDef":null,"DateOfEntry":"/Date(1380515350000)/","LatestModified":false,"FYID":44,"PeriodId":6,"UserId":107},{"CountryID":2,"CountryIDNew":1,"Name":"India","Shname":"IN","Des":null,"InternationalCode":null,"ISDCode":"091","Active":true,"SoftDelete":false,"CountryDef":null,"DateOfEntry":"/Date(1387793898000)/","LatestModified":true,"FYID":44,"PeriodId":9,"UserId":117}]

Thanks In Advance...

  • Check out http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android for answers about how to parse JSON in Android. – Code-Apprentice Apr 05 '14 at 07:33

3 Answers3

0

Try this android code.

 JsonArray resultJson = new JSONArray(json_string);

    for(int i=0;i<resultJson.length();i++)
    {

    JsonObject jobj = (JSONObject) resultJson.get(i);
    String country_id = jobj.getString("CountryID");
    String country_name = jobj.getString("Name");

    }
kaushik parmar
  • 805
  • 6
  • 19
  • "[{"CountryID":1,"CountryIDNew":1,"Name":"India","Shname":"IN","Des":null,"InternationalCode":null,"ISDCode":"091","Active":true,"SoftDelete":false,"CountryDef":null,"DateOfEntry":"/Date(1380515350000)/","LatestModified":false,"FYID":44,"PeriodId":6,"UserId":107},{"CountryID":2,"CountryIDNew":1,"Name":"India","Shname":"IN","Des":null,"InternationalCode":null,"ISDCode":"091","Active":true,"SoftDelete":false,"CountryDef":null,"DateOfEntry":"/Date(1387793898000)/","LatestModified":true,"FYID":44,"PeriodId":9,"UserId":117}]"; This type of String Is not Accepted in Java code For Processing – Deepak.jagtap Apr 05 '14 at 09:29
  • (")should be replace by(\") for processing but how to generate that string from JSON object??? – Deepak.jagtap Apr 05 '14 at 09:32
0

You can use this:

Create a class

   public class Country{ public String name =""; public String countryId =""; }

And create a array list

ArrayList countryList = new ArrayList();

String serverRes = "[{"CountryID":1,"CountryIDNew":1,"Name":"India","Shname":"IN","Des":null,"InternationalCode":null,"ISDCode":"091","Active":true,"SoftDelete":false,"CountryDef":null,"DateOfEntry":"/Date(1380515350000)/","LatestModified":false,"FYID":44,"PeriodId":6,"UserId":107},{"CountryID":2,"CountryIDNew":1,"Name":"India","Shname":"IN","Des":null,"InternationalCode":null,"ISDCode":"091","Active":true,"SoftDelete":false,"CountryDef":null,"DateOfEntry":"/Date(1387793898000)/","LatestModified":true,"FYID":44,"PeriodId":9,"UserId":117}]";


JSONArray response = new JSONArray(serverRes);

    for(int i=0;i<response.length();i++)
    {

    JSONObject jobj =  response.getJSONObject(i);
        Country c = new  Country();
    c.name = jobj.getString("CountryID");
    c.countryId = jobj.getString("Name");
         countryList.add(c);
    }

And you can get array list with data.

Yogendra
  • 4,817
  • 1
  • 28
  • 21
0

use this code

JsonArray resultJson = new JSONArray(json_string);

for(int i=0;i<resultJson.length();i++)
{
     JsonObject jobj = resultJson.getJSONObject(i);
     if( jobj.has("CountryID") )
     {
           String country_id = jobj.getString("CountryID");
     }
     if( jobj.has("Name") )
      {
           String country_name = jobj.getString("Name");
     }
}

hope this help you

Zeeshan
  • 1,625
  • 17
  • 25