简体   繁体   English

使用Java生成有效JSON的异常

[英]Exception casting valid JSON with Java

I have a JSON string : 我有一个JSON字符串

{
    "_id": -1,
    "create_date": 0,
    "update_date": 0,
    "description": "test",
    "active": true
}

In Java, I try to parse it using org.json.simple.parser.JSONParser : 在Java中,我尝试使用org.json.simple.parser.JSONParser解析它:

JSONParser jsonParser = new JSONParser();
org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject) jsonParser.parse(phoneJSON);

I try to retrieve the _id field value: 我尝试检索_id字段值:

String s_id = ((String) jsonObject.get("_id"));

but encounter the following exception: 但遇到以下异常:

java.lang.Long cannot be cast to java.lang.String

Furthermore, if I display the field value in the console: 此外,如果我在控制台中显示字段值:

System.out.println("print value -  _id: "+jsonObject.get("_id"));

I get: 我明白了:

print value -  _id: -1

output in the console. 控制台中的输出。

I have seen this post: 我看过这篇文章:

java.lang.Long cannot be cast to java.lang.String java.lang.Long无法强制转换为java.lang.String

But is does not help me. 但是对我没有帮助。

What am I not understanding? 我不明白的是什么?

The problem is not in the parsing process. 问题不在解析过程中。 But in the casting process of the following line: 但是在以下线的铸造过程中:

String s_id = ((String) jsonObject.get("_id"));

jsonObject.get("_id") returns - as specified by the error - a java.lang.Long , and you later cast it to String . jsonObject.get("_id")返回 - 由错误指定 - java.lang.Long ,稍后将其jsonObject.get("_id")String

You can solve this, for instance with: 你可以解决这个问题,例如:

String s_id = jsonObject.get("_id").toString();

In Java the (String) is not a conversion , but a downcast. 在Java中, (String) 不是转换 ,而是转发。 Since a String is not a subclass of Long , it will error. 由于String不是Long的子类,因此会出错。 You can however call .toString() that will transform the Long in a textual representation. 但是,您可以调用.toString() ,它将以文本表示形式转换Long


The reason: 原因:

System.out.println("print value -  _id: "+jsonObject.get("_id"));

works is because if you "add" an object to a string, the toString method is called automatically. 工作是因为如果您将一个对象“添加”到一个字符串,则会自动调用toString方法。 Implicitly you have written: 隐含地你写了:

System.out.println("print value -  _id: "+Objects.toString(jsonObject.get("_id")));

You have to use the .toString() method to convert Long to String. 您必须使用.toString()方法将Long转换为String。

String strLong = Long.toString(jsonObject.get("_id")));

Reference : 参考

Returns a String object representing this Long's value. 返回表示此Long值的String对象。

Also, the reason println outputs the value in the console is because PrintStream.println has an overload that takes an Object , and then calls its toString method. 另外,其原因println输出在控制台的值是因为PrintStream.println具有重载需要Object ,然后调用其toString方法。

The value of _id is correctly identified as Long (-1 in the example) when you use jsonObject.get("_id") . 使用jsonObject.get("_id")时,_id的值被正确标识为Long(示例中为-1 jsonObject.get("_id")

If you want a String use jsonObject.getString("_id"). 如果你想要一个String使用jsonObject.getString("_id").

Why do you except the "_id" field to be a string? 你为什么除了“_id”字段是一个字符串?

Its value is -1 (opposed to "-1") so it is actually a Long instead of String. 它的值为-1(与“-1”相反),因此它实际上是Long而不是String。

However, if you need string, you can cast it via 但是,如果你需要字符串,你可以通过它来投射它

String s_id = String.valueOf(jsonObject.get("_id"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM