Gson,解析json内部类列表,javabean

时间:2014-09-30 11:31:40

标签: java json gson inner-classes

那么我正在尝试解析一些JSon。我成功地解析了:

Member.json:

{"member":{"id":585897,"name":"PhPeter","profileIconId":691,"age":99,"email":"peter@adress.com "}}

但如果我需要解析怎么办?

{"Members":[{"id":585897,"name":"PhPeter","profileIconId":691,"age":99,"email":‌​‌​"peter@adress.com"},{"id":645231,"name":"Bill","profileIconId":123,"age":56,"em‌​ai‌​l":"bill@adress.com"}]}

当然我在网上搜索,我想,我需要使用“List&lt;&gt;”这里private List<memberProfile> member;但我如何从我的主班“得到”这个?

我用它来解析第一个字符串:

memeberClass.java

public class memberClass {

    private memberProfile member;   

    public memberProfile getMember() {  
        return member;  
    }  
    public class memberProfile{
        int id;
        String name;
        int profileIconId;
        int age;
        String email;

        //Getter
        public int getId() {  
            return id;  
        }  
        public String getName() {  
            return name;  
        } 
        public int getProfileIconId() {  
            return profileIconId;  
        }  
        public int getAge() {  
            return age;  
        }  
        public String getEmail() {  
            return email;  
        } 
    }
}

memberToJava.java

public class memberToJava {

    public static void main(String[] args) {

        Gson gson = new Gson();

        try {  

            BufferedReader br = new BufferedReader(new FileReader("...Member.json"));  

            //convert the json string back to object  
            memberClass memberObj = gson.fromJson(br, memberClass.class);

            System.out.println("Id: " + memberObj.getMember().getId());
            System.out.println("Namw: " + memberObj.getMember().getName());
            System.out.println("ProfileIconId: " + memberObj.getMember().getProfileIconId());
            System.out.println("Age: " + memberObj.getMember().getAge());
            System.out.println("Email: " + memberObj.getMember().getEmail());


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

2 个答案:

答案 0 :(得分:1)

您好我对您的应用程序进行了一些更改,它现在似乎正常工作!你需要的所有东西都是数组的包装器。

public class memberWrapper {

    private List<memberClass> Members;

    public List<memberClass> getMembers() {
        return Members;
    }

    public void setMembers(List<memberClass> members) {
        this.Members = members;
    }
}

然后我改变了原来的课程:

public class memberClass {
    int id;
    String name;
    int profileIconId;
    int age;
    String email;

    //Getter
    public int getId() {  
        return id;  
    }  
    public String getName() {  
        return name;  
    } 
    public int getProfileIconId() {  
        return profileIconId;  
    }  
    public int getAge() {  
        return age;  
    }  
    public String getEmail() {  
        return email;  
    } 
}

然后在主要:

BufferedReader br = new BufferedReader(new FileReader("stuff.json"));  

//convert the json string back to object  
memberWrapper memberObj = gson.fromJson(br, memberWrapper.class);

System.out.println("Id: " + memberObj.getMembers().get(0).getId());

现在处理JSOn时,重要的是确保密钥与变量名匹配。

答案 1 :(得分:1)

见下面的代码

<强> MemberClass.java

import java.util.List;

public class MemberClass {

    private List<MemberProfile> member;

    public List<MemberProfile> getMember() {
        return member;
    }

    public void setMember(List<MemberProfile> member) {
        this.member = member;
    }

    public class MemberProfile {
        int id;
        String name;
        int profileIconId;
        int age;
        String email;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getProfileIconId() {
            return profileIconId;
        }

        public void setProfileIconId(int profileIconId) {
            this.profileIconId = profileIconId;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

    }
}

主要类

import com.google.gson.Gson;


public class MemTest {
    public static void main(String[] args) {
        String json = "{'member':[{'id':585897,'name':'PhPeter','profileIconId':691,'age':99,'email':‌​‌​'peter@adress.com'},{'id':645231,'name':'Bill','profileIconId':123,'age':56,'em‌​ai‌​l':'bill@adress.com'}]}";
        MemberClass memberClass = new Gson().fromJson(json, MemberClass.class);

        System.out.println(new Gson().toJson(memberClass));
    }
}

<强>输出

{"member":[{"id":585897,"name":"PhPeter","profileIconId":691,"age":99,"email":"‌​‌​\u0027peter@adress.com\u0027"},{"id":645231,"name":"Bill","profileIconId":123,"age":56}]}