5

I have a problem parsing JSON integer in my REST service. Parsing String and double type works fine

Working:

JSONParser parser = new JSONParser();
Object obj = null;
try {
    obj = parser.parse(input);
} catch (ParseException e) {
    e.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;   

//---------------
String uName = (String) jsonObject.get("userName");
double iPrice = (Double) jsonObject.get("itemPrice");

Not working:

int baskId = (Integer) jsonObject.get("basketId");

I tried to convert the basketId in my basket class to String, and then it functions ok, so code is okay, and link is working, however, when I cast it back to int, I get 500 server error. I am using it to create a new basket with some numeric ID, so I use the @POST annotation and the JSON in the payload is as follows:

{"basketId":50}

I don't get it...

EDIT: I do get it...JSON simple accepts just bigger types of Java primitives, so integer and float are a no-no

6
  • What is error message? Commented Apr 21, 2015 at 11:02
  • 1
    What library do you use ? Doesn't it have a getInt(...) method ? Commented Apr 21, 2015 at 11:03
  • Its JSON simple library, code.google.com/p/json-simple, no getInt, just get. Double gets parsed fine. I am using chrome rest client plugin, so i get 500 Internal server error. There is no error in eclipse Commented Apr 21, 2015 at 11:09
  • Integer.parseInt().. stackoverflow.com/questions/15699953/… Commented Apr 21, 2015 at 11:15
  • 1
    In that case you have to check the type of the value basketId before you cast it. You can do that using instanceof or jsonObject.get("basketId").getClass() Commented Apr 21, 2015 at 11:17

3 Answers 3

7

In your code jsonObject.get("basketId");returns Long

So using Long for type casting would help you in resolving your error (Long)jsonObject.get("basketId");

If you really need Integer then type cast it to inetger as follows

((Long)jsonObject.get("basketId")).intValue()
Sign up to request clarification or add additional context in comments.

1 Comment

This actually works...hmm...it is possible that JSON Simple accepts just bigger types, since I just tried with float, and it doesn't work either.
0

If you parse it as String, you cant do something like this :

String x = "10";
int y = (int) x;

But you can use this method

String x = "10";
int y = Integer.valueOf(x);

2 Comments

It doesn't do me good, since basket constructor is Basket(int basketId)...I would have to put then all kinds of limitations client-side, and do the conversions both ways
Well, we can use jackson, than the de/serialization is done automatically
0

Rather than : int baskId = (Integer) jsonObject.get("basketId");

Use : int baskId = jsonObject.getInt("basketId");

It is in the official documentation : http://www.json.org/javadoc/org/json/JSONObject.html#getInt(java.lang.String)

1 Comment

He doesn't use that library, he uses code.google.com/p/json-simple and in this library JSONObject is just an extension of Map and it doesn't have a getInt(...) method.

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.