简体   繁体   中英

Java JSONArray from Javascript JSONArray

I am passing a json object from javascript to a java servlet using ajax.

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.

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.

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

Here is what the json array looks like when its passed in from javascript. This is a println of the jsonArray object.

[{"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.

[[{"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? Using just the dataObj I have no access to size or get to loop through it.

    [
    [
        {
            "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:

[{"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. Note that string values, lons and booleans are not JSONObjects. I would suggest you to use the more genereal JSONArray.get and check instance of what class it is. Maybe this can head you to the problem with the json you have. 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. as the other answer mentioned - this is array of arrays. Also consider changing the javascript to reduce the level of arrays included.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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