Exception casting valid JSON with Java -
i have json string:
{ "_id": -1, "create_date": 0, "update_date": 0, "description": "test", "active": true } in java, try parse using org.json.simple.parser.jsonparser :
jsonparser jsonparser = new jsonparser(); org.json.simple.jsonobject jsonobject = (org.json.simple.jsonobject) jsonparser.parse(phonejson); i try retrieve _id field value:
string s_id = ((string) jsonobject.get("_id")); but encounter following exception:
java.lang.long cannot cast java.lang.string furthermore, if display field value in console:
system.out.println("print value - _id: "+jsonobject.get("_id")); i get:
print value - _id: -1 output in console.
i have seen post:
java.lang.long cannot cast java.lang.string
but not me.
what not understanding?
you have use .tostring() method convert long string.
string strlong = long.tostring(jsonobject.get("_id"))); returns string object representing long's value.
also, reason println outputs value in console because printstream.println has overload takes object, , calls tostring method.
Comments
Post a Comment