解析包含对象数组的Json字符串和从Json String获取特定值

时间:2014-04-05 07:24:17

标签: java android json string parsing

这是我的Json String。我想提取每个对象的国家名称和国家ID .. 请帮我一样

字符串===

[{ “CountryID”:1, “CountryIDNew”:1, “名称”: “印”, “Shname”: “IN”, “辅”:NULL, “InternationalCode”:NULL, “ISDCode”:” 091" , “活动”:真实的, “SoftDelete”:假的, “CountryDef”:空, “DateOfEntry”: “/日期(1380515350000)/”, “LatestModified”:假的, “FYID”:44, “PeriodId”: 6, “用户ID”:107},{ “CountryID”:2 “CountryIDNew”:1, “名称”: “印”, “Shname”: “IN”, “辅”:NULL, “InternationalCode”:空, “ISDCode”: “091”, “活动”:真实的, “SoftDelete”:假的, “CountryDef”:空, “DateOfEntry”: “/日期(1387793898000)/”, “LatestModified”:真实的, “FYID”:44 “PeriodId”:9 “用户ID”:117}]

提前致谢...

3 个答案:

答案 0 :(得分:0)

试试这个Android代码。

 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");

    }

答案 1 :(得分:0)

您可以使用:

创建一个类

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

创建一个数组列表

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);
    }

你可以获得包含数据的数据列表。

答案 2 :(得分:0)

使用此代码

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");
     }
}

希望这能帮到你