1

I have this class:

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

I have a JSON string that I'm converting to a MyClass object by doing:

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

However, I'm getting the following exception:

Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $

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

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

What am I doing wrong?

EDIT

As requested, this is the complete code:

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

And the update class is:

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

    @SerializedName("Date")
    public long date;
}
3
  • I am not sure...but as I can see your error tells that your "Number" is int is you are extracting String Commented Oct 26, 2015 at 10:58
  • 1
    String s = "{\"Number\":2,\"Date\":1444953600}"; change the above line to match the case String s = "{\"number\":2,\"date\":1444953600}"; Commented Oct 26, 2015 at 10:59
  • var obj = JSON.parse(s) Commented Oct 26, 2015 at 10:59

5 Answers 5

2

Use annotation @SerializedName, like:

public class MyClass {
    @SerializedName("Number")
    public int number;
    @SerializedName("Date")
    public long date;
}
Sign up to request clarification or add additional context in comments.

6 Comments

I did that and I still get the same exception.
@Ivan-Mark Debono, i tryied your code with annotation, and it's Ok... Strange you have error...
@Ivan-Mark Debono show all your code in file where MyClass locate
That's all the code the class contains. Could it be because of `\` ?
@Ivan-Mark Debono No, i copied your class and string absolutly, and haven't error, with annotation
|
0
String s = "{\"number\":2,\"date\":1444953600}";
tp temp = new Gson().fromJson(s, tp.class);

chack upper and lower case of variable names

2 Comments

Tried that too. Still get the same exception.
I did run this code and it ran successfully. could you share complete code?
0

You need to do something like this: (Assuming that you're actually going to need to do it for multiple entries.)

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

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

Then in your other class, to convert into an object:

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

With: (Will possibly need to be surrounded by try&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"));

Hope this helps, it might be slightly more complicated than you need but it means that it's scalable.

Comments

0

try without the capital letters :

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

edit: i tried this like that:

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

and it works

Comments

0

Try this -

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

Result

ElemntList [Number=2, Date=1444953600]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.