Java Android:将JSON对象值转换为指定类

Java Android:将JSON对象值转换为指定类,java,android,json,Java,Android,Json,我不熟悉Android开发和JAVA。我需要将JSONObject值传递到android类中。我知道一些外部库,比如GSON和其他库(比如Jackson),但对于这个任务,我只使用JSON。到目前为止,我已经创建了一个JSONObject: public void jsonObject(){ JSONObject test1 = new JSONObject(); try{ test1.put("name", "Pete"); test1.put

我不熟悉Android开发和JAVA。我需要将JSONObject值传递到android类中。我知道一些外部库,比如GSON和其他库(比如Jackson),但对于这个任务,我只使用JSON。到目前为止,我已经创建了一个JSONObject:

public void jsonObject(){

    JSONObject test1 = new JSONObject();
    try{
        test1.put("name", "Pete");
        test1.put("surname", "Thompson");
        test1.put("age", "24");
        test1.put("height","182");
        test1.put("id", "45");
    }
    catch (JSONException a){
        a.printStackTrace();
    }
}
从该对象,我需要将数据传递给以下类成员:

private String name;
private String surname;
private int age;
private int height;
private long id;

提前感谢。

然后为您的类创建适当的构造函数:

public class Test {
    String name;
    String surname;
    int age;
    int height;
    long id;

    public Test(JSONObject jsonObject) {
        this.name = jsonObject.getString("name");
        this.surname = jsonObject.getString("surname");
        this.age = jsonObject.getInt("age");
        this.height = jsonObject.getInt("height");
        this.id = jsonObject.getLong("id");
    }
}

然后为您的类创建适当的构造函数:

public class Test {
    String name;
    String surname;
    int age;
    int height;
    long id;

    public Test(JSONObject jsonObject) {
        this.name = jsonObject.getString("name");
        this.surname = jsonObject.getString("surname");
        this.age = jsonObject.getInt("age");
        this.height = jsonObject.getInt("height");
        this.id = jsonObject.getLong("id");
    }
}

使用GSON时的工作负载如下所示:

使用GSON生成的对象:

class MyUser{

    @Expose @SerializedName("name");
    private String name;

    @Expose @SerializedName("surname");
    private String surname;

    //and so on

    public String getName(){
        return name;
    }

    public static MyUser fromJson(String json){
        return new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                    .create().fromJson(json, MyUser.class);
    }

}
如何使用:

class WorkWithObjects{
    private MyUser currentUser;

    public void dataRecieved(String json){
        currentUser = MyUser.fromJson(json);
        Log.i("NAME", "IS " + currentUser.getName());
    }

}

使用GSON时的工作负载如下所示:

使用GSON生成的对象:

class MyUser{

    @Expose @SerializedName("name");
    private String name;

    @Expose @SerializedName("surname");
    private String surname;

    //and so on

    public String getName(){
        return name;
    }

    public static MyUser fromJson(String json){
        return new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                    .create().fromJson(json, MyUser.class);
    }

}
如何使用:

class WorkWithObjects{
    private MyUser currentUser;

    public void dataRecieved(String json){
        currentUser = MyUser.fromJson(json);
        Log.i("NAME", "IS " + currentUser.getName());
    }

}

嘿,这是gson提供的,看看外面的一些图坦卡门,你会喜欢的!是的,我使用过GSON ext.lib,但现在我只能使用内置lib。为了更好地理解这一切是如何运作的。。。我想你会得到的;)嘿,这正是gson提供的,看看外面的一些图坦卡门,你会喜欢它的!是的,我使用过GSON ext.lib,但现在我只能使用内置lib。为了更好地理解这一切是如何运作的。。。我想你会得到的;)HF但是字段是私有的,需要使用getter和setter,对吗?@russell1911dev如果你想保持它们的私有性,是的,getter和setter是必需的(但是请记住,Android中不推荐使用它们)。或者您可以使它们包可见或公开。您能给我一些示例,说明如何使用system.out.print将来自JSONObject的信息放入getter和setter并打印到控制台中吗?我不会问,但我得到了nullpointerException。@russell1911dev我已经使它们受到包保护(没有访问修饰符),所以您不需要getter和setter。请注意,您不能在Android上使用System.out,请改用Log class()。如果出现错误,最好更新您的问题。但是字段是私有的,需要使用getter和setter,对吗?@russell1911dev如果您想让它们私有,是的,需要getter和setter(但请记住,Android中不推荐使用)。或者您可以使其包可见或公开。您能给我一些示例,说明如何使用system.out.print将JSONObject中的信息放入getter和setter,并打印到控制台中吗?我不会问,但我得到nullpointerException。@russell1911dev我已使其包受保护(没有访问修饰符),所以您不需要getter和setter。请注意,您不能在Android上使用System.out,请改用Log class()。如果您遇到错误,最好更新您的问题。