简体   繁体   English

Java语法:JSONArray array =(JSONArray)obj

[英]Java Syntax: JSONArray array = (JSONArray)obj

I'm learning to Decode JSON files in Java and have come across some Syntax I do not understand. 我正在学习用Java解码JSON文件,并且遇到了一些我不理解的语法。 I am also new to Java. 我也是Java的新手。 Here is the code snippet: 这是代码片段:

Object obj = JSONValue.parse(jsonResult);
JSONArray array = (JSONArray)obj;

In my best attempt at Programmer speak, I understand that "JSONArray" is a class. 在我对程序员的最大尝试下,我了解“ JSONArray”是一个类。 We are instantiating a new JSONArray and calling it "array". 我们正在实例化一个新的JSONArray并将其称为“数组”。 We are initializing "array" with the value on the right side of the equal sign. 我们正在使用等号右侧的值初始化“数组”。

My question is -- I don't understand what is happening on the right side of the equal sign. 我的问题是-我不明白等号右侧发生了什么。 Why is "JSONArray" in parenthesis: (JSONArray)obj? 为什么括号中的(JSONArray)obj为“ JSONArray”? I don't understand what is happening here. 我不明白这里发生了什么。

Thanks! 谢谢!

That is an example of an (unchecked) type-cast. 这是(未检查的)类型转换的示例。 I suggest you use a checked cast using the type comparison operator instanceof first. 我建议您首先使用类型比较运算符instanceof使用检查过的类型转换。 Something like, 就像是,

JSONArray array = null;
Object obj = JSONValue.parse(jsonResult);
if (obj instanceof JSONArray) {
    array = (JSONArray)obj;
}

If you're certain it will always be a JSONArray you might do it in one line like 如果确定它将始终是JSONArray ,则可以在一行中执行,例如

JSONArray array = (JSONArray) JSONValue.parse(jsonResult);

The reason it's necessary to cast at all is because the result of parse() is an Object . 之所以必须进行强制转换,是因为parse()的结果是一个Object

Received JSON may, as its outermost structure, be either an "object" (Map) or "array" (List). 作为最外层结构,接收到的JSON可以是“对象”(地图)或“数组”(列表)。 JSONValue.parse(jsonResult) produces either a JSONObject or JSONArray, depending on which sort of structure is outermost in the jsonResult string. JSONValue.parse(jsonResult)生成JSONObject或JSONArray,具体取决于jsonResult字符串中最外面的结构类型。 So the parse method must be declared to return some common "ancestor" of those two classes. 因此,必须声明parse方法以返回这两个类的某个公共“祖先”。 JSON-Simple is a rather crude JSON kit which has no common superclass for those two classes other than Object. JSON-Simple是一个相当粗糙的JSON套件,除了Object以外,这两个类没有通用的超类。

Presumably the programmer in this case knows that the received data will always have a JSON "array" as the outermost structure. 在这种情况下,程序员大概知道接收到的数据将始终具有JSON“数组”作为最外层结构。 He initially puts the result from parse in an Object reference (since Object is the formal type returned from parse ), then casts that value to JSONArray. 他最初将来自parse的结果放入一个Object引用中(因为Object是从parse返回的形式类型),然后将该值转换为JSONArray。

As Eliot suggests, it would have been better, formally, at least, to include an instanceof test, or, if one used a kit other than JSON-Simple, one could possibly make use of interfaces on a common superclass for the two classes that allowed querying their types. 正如艾略特(Eliot)所建议的那样,至少从形式上来讲,最好包含一个instanceof ,或者,如果使用的不是JSON-Simple套件,则可以为这两个类使用通用父类上的接口允许查询其类型。

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

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