将简单的JSON字符串转换为对象

时间:2015-10-26 10:55:45

标签: java android json gson

我有这堂课:

public class MyClass {
    public int number;
    public long date;
}

我有一个JSON字符串,我通过执行以下操作转换为MyClass对象:

String s = "{\"Number\":2,\"Date\":1444953600}";
MyClass temp = new Gson().fromJson(s, MyClass.class);

但是,我遇到以下异常:

  

引起:java.lang.IllegalStateException:预期BEGIN_OBJECT但是第1行第2行STRING

     

在com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)

     

在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:210)

我做错了什么?

修改

根据要求,这是完整的代码:

URL url = new URL(some_url);
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("GET");

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer response = new StringBuffer();

String inputLine;
while ((inputLine = reader.readLine()) != null) {
    response.append(inputLine);
}
reader.close();

MyClass temp = new Gson().fromJson(response.toString(), MyClass.class);

更新类是:

public class MyClass implements Serializable {
    @SerializedName("Number")
    public int number;

    @SerializedName("Date")
    public long date;
}

5 个答案:

答案 0 :(得分:2)

使用注释@SerializedName,如:

public class MyClass {
    @SerializedName("Number")
    public int number;
    @SerializedName("Date")
    public long date;
}

答案 1 :(得分:0)

String s = "{\"number\":2,\"date\":1444953600}";
tp temp = new Gson().fromJson(s, tp.class);

查看变量名称的大写和小写

答案 2 :(得分:0)

您需要执行以下操作:(假设您实际上需要为多个条目执行此操作。)

public class MyClass{
     public int number;
     public long date;

     MyClass(int num, long Date){
          this.number = num;
          this.date = Date;
     }
}

然后在你的另一个类中,转换成一个对象:

ArrayList<MyClass> list = new ArrayList<>();
list.add(new MyClass(string1, string2));

使用:(可能需要被try&amp; catch包围)

JSONObject Object = new JSONObject(s); // Where s is your string.
String string1 = new String(Object.getString("Number"));
String string2 = new String(Object.getString("Date"));

希望这会有所帮助,它可能比您需要的稍微复杂一些,但这意味着它可以扩展。

答案 3 :(得分:0)

尝试没有大写字母:

String s = "{\"number\":2,\"date\":1444953600}";

编辑: 我这样试过:

import com.google.gson.Gson;

public class App {
public static void main(String[] args) {
    String s = "{number:2,date:1444953600}";
    MyClass temp = new Gson().fromJson(s, MyClass.class);
    System.out.println("number="+temp.number+" , "+temp.date);
}

class MyClass {
    public int number;
    public long date;
}
}

并且有效

答案 4 :(得分:0)

试试这个 -

ElemntList.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ElemntList {
    @SerializedName("Number")
    @Expose
    private Integer Number;
    @SerializedName("Date")
    @Expose
    private Long Date;

    public Integer getNumber() {
        return Number;
    }

    public void setNumber(Integer Number) {
        this.Number = Number;
    }

    public Long getDate() {
        return Date;
    }

    public void setDate(Long Date) {
        this.Date = Date;
    }

    @Override
    public String toString() {
        return "ElemntList [Number=" + Number + ", Date=" + Date + "]";
    }

}

<强> Main.java

import com.example.ElemntList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Main {
    private static Gson gson;

    static {
        gson = new GsonBuilder().create();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String s = "{\"Number\":2,\"Date\":1444953600}";
        ElemntList info = gson.fromJson(s, ElemntList.class);
        System.out.println(info);
   }
}

结果

ElemntList [Number=2, Date=1444953600]