从JSON读取布尔值

时间:2016-06-21 08:52:48

标签: android json

我使用以下json作为我的Android应用程序。

[
{
    "id" : "001",
    "firstName" : "Mark",
    "lastName" : "Mason",
    "role" : "CEO",
    "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
    "teamName" : "iOS",
    "members" : [
        {
            "id" : "002",
            "firstName" : "Olly",
            "lastName" : "Berry",
            "role" : "iOS Team Lead",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png",
            "teamLead" : true
        },
        {
            "id" : "003",
            "firstName" : "James",
            "lastName" : "Frost",
            "role" : "iOS Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "004",
            "firstName" : "Liam",
            "lastName" : "Nichols",
            "role" : "iOS Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "005",
            "firstName" : "Chris",
            "lastName" : "Watson",
            "role" : "iOS Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "006",
            "firstName" : "Richard",
            "lastName" : "Turton",
            "role" : "iOS Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "007",
            "firstName" : "Matt",
            "lastName" : "Colliss",
            "role" : "iOS Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "008",
            "firstName" : "David",
            "lastName" : "Gibson",
            "role" : "iOS Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "009",
            "firstName" : "Tom",
            "lastName" : "Guy",
            "role" : "iOS Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "010",
            "firstName" : "Rich",
            "lastName" : "Hodgkins",
            "role" : "iOS Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        }
    ]
},
{
    "teamName" : "Android",
    "members" : [{
            "id" : "011",
            "firstName" : "David",
            "lastName" : "Branton",
            "role" : "Android Team Lead",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png",
            "teamLead" : true
        },
        {
            "id" : "012",
            "firstName" : "Dre",
            "lastName" : "Pilipczuk",
            "role" : "Android Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "013",
            "firstName" : "Ray",
            "lastName" : "Britton",
            "role" : "Android Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "014",
            "firstName" : "Charly",
            "lastName" : "Murillo",
            "role" : "Android Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        }
    ]   
},
{
    "teamName" : "Web",
    "members" : [{
            "id" : "015",
            "firstName" : "Ryan",
            "lastName" : "French",
            "role" : "Web Team Lead",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png",
            "teamLead" : true
        },
        {
            "id" : "016",
            "firstName" : "James",
            "lastName" : "Ward",
            "role" : "Web Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "018",
            "firstName" : "Adam",
            "lastName" : "Smith",
            "role" : "Web Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "019",
            "firstName" : "Leonard",
            "lastName" : "Da Costa",
            "role" : "Web Developer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        }
    ]   
},
{
    "teamName" : "Design",
    "members" : [{
            "id" : "020",
            "firstName" : "Hannah",
            "lastName" : "Tempest",
            "role" : "Design Team Lead",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png",
            "teamLead" : true
        },
        {
            "id" : "021",
            "firstName" : "Ellis",
            "lastName" : "Reed",
            "role" : "Designer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "022",
            "firstName" : "Pete",
            "lastName" : "Horsham",
            "role" : "Designer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "023",
            "firstName" : "Hemel",
            "lastName" : "Dave",
            "role" : "Designer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        },
        {
            "id" : "024",
            "firstName" : "Hannah",
            "lastName" : "Corke",
            "role" : "Designer",
            "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
        }
    ]   
}

我在recyclerview中成功显示这些数据。但是,我想强调主要摄影师的排。主要摄影师对象有一个" teamLead"对象,其他人不要。

所以如果我把它设置为真。

for(int j=0;j<teamMembersArray.length();j++){

 //The model class which contains the setters/getters
 //in order to "deserialize" the JSON objects into string,int,boolean   
 //objects.
          Model m = new Model();


          JSONObject teamObject = teamMembersArray.getJSONObject(j);


          m.setId(teamObject.getInt("id"));
          //This line refers to ALL JSON OBJECTS!!! I want 
          //to read only those who had teamLead separately from
          //the other objects.

        if(teamObject.has("teamLead")){
                               m.setTeamLead(teamObject.getBoolean("teamLead"));
                           }else {

                               m.setId(teamObject.getInt("id"));
                               m.setProfileImageURL(teamObject.getString("profileImageURL"));
                               m.setFirstName(teamObject.getString("firstName"));
                               m.setLastName(teamObject.getString("lastName"));
                               m.setRole(teamObject.getString("role"));


                               //Finally I am adding the string objects into an ArrayList.
                               modelArrayList.add(m);

                           }

我得到一个例外,说teamLeader JSONObject不存在。这是令人困惑和混乱的情况。

我该如何解决?

谢谢,

西奥。

1 个答案:

答案 0 :(得分:1)

  

例外说teamLead JSONObject,不存在

由于teamLead JSONObject's中的teamMembersArray密钥在JSONArray teamLead中的所有JSONObject都不可用。

因此,要使其工作,请在从if (teamObject.has("teamLead") && !teamObject.isNull("teamLead")) { m.setTeamLead(teamObject.getBoolean("teamLead")); }else{ m.setTeamLead(false); // set default value here } 访问{{1}}密钥的值之前添加check for null:

{{1}}