简体   繁体   English

将JSONArray转换为适当类型的Java数组

[英]Convert JSONArray to appropriately-typed Java array

I'm struggling at integrating some existing pieces of code with a server that accepts inputs via JSON payload. 我正在努力将一些现有的代码片段与通过JSON负载接受输入的服务器进行集成。 I am handed a JSON object, and I need to construct a Map that represents the key-value pairs, where the values take on reasonable, expected types. 我得到了一个JSON对象,我需要构造一个表示键-值对的Map,其中的值采用合理的预期类型。 The org.json parser seems adequate for every case except for the one around JSONArrays. org.json解析器似乎适合每种情况,但JSONArrays除外。

I can see that it's quite easy to build a String[] from a JSONArray ... provided you know that a String[] is what you need. 我可以看到,从JSONArray构造String []是很容易的……只要您知道String []是您所需要的即可。 However, I am also handed arrays of doubles, arrays of ints, with nothing to distinguish them. 但是,我还得到了双精度数组,整数数组,没有任何区别。 I need to figure out the type, and create an appropriately-typed array. 我需要弄清楚类型,并创建一个适当类型的数组。

I've tried pulling the first element from the array and getting its type, in hopes of creating an array instance of that type, like so: 我尝试从数组中提取第一个元素并获取其类型,以期创建该类型的数组实例,如下所示:

private Object[] decodeToArray(JSONArray list) throws JSONException { 

    if (list.length() > 0) {
        Class<? extends Object> type = list.get(0).getClass();
        type[] result = Array.newInstance(type, list.length());
    }

But of course, this doesn't compile -- type is an instance, not a class; 但是,当然,这不能编译- type是实例,而不是类; it won't accept ? 它不会接受? as a type; 作为一种类型 and if I try to give it a name, a la <T extends Object> , it requires the T to be declared in the method signature, which doesn't seem to be connected in any way to the unknown type obtained from the list element. 如果我尝试给它起一个名字,la <T extends Object> ,它要求在方法签名中声明T ,它似乎丝毫没有连接到从list元素获得的未知类型。 。

And this, of course, doesn't do anything at all for the int[] and double[] case. 当然,这对于int[]double[]情况完全不起作用。 Integer[] and Double[] would be acceptable compromises, probably. Integer []和Double []可能是可以接受的折衷方案。

As an interim solution, I've been able to render the JSONArray as an Object[], and then change the server-side code that interacts with these arrays to cast them as needed, but it's ugly, and it allows some JSON-yuck to seep into our code base. 作为一种临时解决方案,我已经能够将JSONArray呈现为Object [],然后更改与这些数组进行交互的服务器端代码以根据需要进行强制转换,但这很丑陋,并且允许进行JSON-yuck渗入我们的代码库。

Are there any libraries or clever tricks for creating arrays of unknown type in Java? 是否有用于创建Java中未知类型数组的库或巧妙技巧? Is there a JSON convention I can ask to be used that would inform me what type is expected? 是否有我可以要求使用的JSON约定,该约定将告知我期望的类型?

Assuming this is for the real world and not homework, just use Gson , a library that does exactly what you're looking for. 假设这是针对现实世界的,而不是针对家庭作业的,只需使用Gson ,它完全Gson您的需求。

Also, read this thread thoroughly: https://stackoverflow.com/questions/338586/a-better-java-json-library?rq=1 另外,请仔细阅读以下主题: https : //stackoverflow.com/questions/338586/a-better-java-json-library?rq=1

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

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