简体   繁体   English

Javascript JSONArray中的Java JSONArray

[英]Java JSONArray from Javascript JSONArray

I am passing a json object from javascript to a java servlet using ajax. 我正在使用ajax将json对象从javascript传递到Java servlet。

var jsonObj = JSON.stringify(objArray); //Then I pass it to Java using ajax.

In my Java I am getting the json string from the request, then creating a jsonarray, then looping through that array and i'm getting errors when trying to pull one of the json objects from the array. 在我的Java中,我从请求中获取json字符串,然后创建一个jsonarray,然后遍历该数组,尝试从数组中提取json对象之一时遇到错误。

String dataObj = request.getParameter("obj");
String sql = request.getParameter("sql");
ArrayList<Object> returnArray = new ArrayList<Object>();
int key;

//Get type of object being passed.
JSONArray jsonArray = JSONArray.fromObject(dataObj);    
for(int i=0; i<jsonArray.size(); i++) {
    String obj = new Gson().toJson(jsonArray.getJSONObject(i)); //This is where i'm getting an error
    String className = getClassName(jsonArray.getJSONObject(i));

    Class targetClass = null;
        try {
            targetClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    //Create Object
    Object data = new Gson().fromJson(obj, targetClass);

I'm posting the relevant code, the for loop isn't closed because the rest of the code is quite long, and this is the part where i'm getting the error. 我正在发布相关代码,因为其余的代码很长,所以for循环没有关闭,这是我收到错误的部分。

net.sf.json.JSONException: JSONArray[0] is not a JSONObject.

Here is what the json array looks like when its passed in from javascript. 这是json数组从javascript传入时的样子。 This is a println of the jsonArray object. 这是jsonArray对象的println。

[{"number":"(123) 456-7050","type":"Home","contactId":1,"id":16662,"className":"beans.PhoneNumber","position":0}]

With one object in it, this code works. 有了一个对象,此代码即可工作。 But as soon as I get 2 or more, my error comes up. 但是一旦我得到2或更多,我的错误就会出现。

[[{"number":"(123) 456-7050","type":"Home","contactId":1,"id":16662,"className":"beans.PhoneNumber","position":1},{"number":"(555) 555-1233","type":"Mobile","contactId":1,"id":16656,"className":"beans.PhoneNumber","position":0},{"number":"(999) 999-9999","type":"Home","contactId":1,"id":16664,"className":"beans.PhoneNumber","position":3},{"number":"(222) 222-2222","type":"Home","contactId":1,"id":16666,"className":"beans.PhoneNumber","position":4}]]

It almost looks like when i'm passing more than one object, it create an array of an array, which could be why its not working. 当我传递多个对象时,它几乎看起来像是创建了一个数组数组,这可能就是为什么它不起作用的原因。 But how do I avoid doing that when i'm passing a jsonarray from javascript? 但是,当我从JavaScript传递jsonarray时,如何避免这样做? Using just the dataObj I have no access to size or get to loop through it. 我只使用dataObj无法访问大小,也无法遍历它。

    [
    [
        {
            "number":"(123) 456-7050","type":"Home",
            "contactId":1,
            "id":16662,
            "className":"beans.PhoneNumber",
            "position":1
        },
        {
            "number":"(555) 555-1233",
            "type":"Mobile",
            "contactId":1,
            "id":16656,
            "className":"beans.PhoneNumber",
            "position":0
        },
        {
            "number":"(999) 999-9999",
            "type":"Home",
            "contactId":1,
            "id":16664,
            "className":"beans.PhoneNumber",
            "position":3
        },
        {
            "number":"(222) 222-2222",
            "type":"Home",
            "contactId":1,
            "id":16666,
            "className":"beans.PhoneNumber",
            "position":4
        }
    ]
]

This is not an array of objects. 这不是对象数组。 This is an array of arrays of objects. 这是对象数组的数组。 According to your description, you are expecting something like the following to be fed to your Java: 根据您的描述,您期望将类似以下内容的内容提供给您的Java:

[{"foo":"bar"}, {"bar":"baz"}]

But you are really trying to parse: 但是,您实际上是在尝试解析:

[[{"foo":"bar"}, {"bar":"baz"}]]

I am not completely sure, because you have not shared the json that you are trying to parse, but the most probable error you have is just what it says: the first element of the array is not JSONObject. 我不太确定,因为您没有共享要解析的json,但是最可能的错误就是它所说的:数组的第一个元素不是JSONObject。 Note that string values, lons and booleans are not JSONObjects. 请注意,字符串值,lon和boolean不是JSONObjects。 I would suggest you to use the more genereal JSONArray.get and check instance of what class it is. 我建议您使用更通用的JSONArray.get并检查它是什么类的实例。 Maybe this can head you to the problem with the json you have. 也许这可以引导您解决json的问题。 If I got it completely wrong - write back and I will try to help. 如果我完全理解错误,请写回信,我将尽力提供帮助。 In such a case it will be still useful to share the results of the proposed experiment. 在这种情况下,共享建议的实验结果仍然有用。

EDIT : This is double array -> maybe you using getJSONArray(int index) will help you. 编辑 :这是双精度数组->也许您使用getJSONArray(int index)会帮助您。 as the other answer mentioned - this is array of arrays. 正如提到的另一个答案-这是数组的数组。 Also consider changing the javascript to reduce the level of arrays included. 还可以考虑更改javascript以减少包含的数组级别。

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

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