dev@glassfish.java.net

Re: parsing a jsonarray throws exception ?

From: Farjola Zaloshnja <farjola.zaloshnja_at_gmail.com>
Date: Mon, 17 Nov 2008 19:03:44 +0100

Hi,

I am trying to create a JSONArray from a List of entities and I returning
the jsonArray to the client response. When I receive it on the client side
and try to parse the JSONArray and since I do not know anything about the
entities listed, I am considering them as a JSON Object - but then I get
this exception: org.codehaus.jettison.json.JSONException: JSONArray[0] is
not a JSONObject.|#]
The code looks like this on the server:

@Path("getmyentities")
@POST
    public JSONArray getentities(JSONArray ja) throws NumberFormatException,
Exception {
       List<Asset> assetList = new ArrayList<Asset>();
        for (int i = 0; i < ja.length(); i++) {
            logger.log(Level.INFO, "Entry to for loop");
            Long assetid = ja.getLong(i);
            Asset entity = assetManager.getAsset(assetid);
           assetList.add(entity);
        }

        JSONArray jarray = new JSONArray(assetList);
         return jarray;
    }

and on the client it looks like:

             ja = resp.getEntity(JSONArray.class);
            logger.log(Level.INFO, "Size of json array: " + ja.length());
            for (int i = 0; i < ja.length(); i++) {
            logger.log(Level.INFO, "Get Entities():");
                JSONObject obj = ja.getJSONObject(i);
             }
The entities are serializable and they are annotated with JAXB annotations -
but I am definitely missing something .. any ideas please ?

Thanks,
Farjola



On Fri, Nov 14, 2008 at 11:17 AM, Farjola Zaloshnja <
farjola.zaloshnja_at_gmail.com> wrote:

> Thank you Paul, that did the trick!
> It's now that I am using intensively the client api so I am just starting
> to get the hang of it.
>
> Cheers, Farjola
>
> On Fri, Nov 14, 2008 at 10:28 AM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
>
>>
>> On Nov 14, 2008, at 10:05 AM, Farjola Zaloshnja wrote:
>>
>> will this work ?
>>
>>
>> ClientResponse resp = c.handle(cr);
>>
>> JSONObject jobj =
>> *new* JSONObject(resp.toString());
>>
>> JSONArray jas =
>> *new* JSONArray().put(jobj);
>>
>>
>> No. I am guessing you want to receive back a JSONArray from the POST.
>>
>> Client c = Client.create();
>> WebResource r = c.resource("http://host/path");
>>
>> JSONArray ja = new JSONArray(Arrays.asList("foo", "bar"));
>> ja = r.type("application/json").post(JSONArray.class, ja);
>>
>> With a resource class with the following resource method:
>>
>> @Path("path")
>> @POST
>> @Consumes("application/json");
>> public JSONArray post(JSONArray ja) {
>> return ja;
>> }
>>
>> If you want to get the full ClientResponse to get access to the status
>> code and response headers do:
>>
>> JSONArray ja = new JSONArray(Arrays.asList("foo", "bar"));
>> ClientResponse cr = r.type("application/json").post(ClientResponse.class,
>> ja);
>> ja = cr.getEntity(JSONArray.class);
>>
>>
>> Note that it is not necessary to explicitly build a ClientRequest, it is
>> built implicitly when you use WebResource (and the uniform interface).
>>
>>
>> Online JavaDoc for the client is here:
>>
>>
>> https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0/api/jersey/com/sun/jersey/api/client/package-summary.html
>>
>> and you can download it from here:
>>
>>
>> http://download.java.net/maven/2/com/sun/jersey/jersey-client/1.0/jersey-client-1.0-javadoc.jar
>>
>> If you are using NetBeans and the Maven plugin then in the Libraries of
>> your project NetBeans will recognize there is JavaDoc present and it is
>> really easy to browse by selecting View JavaDoc from the right mouse click
>> menu.
>>
>> Paul.
>>
>> On Thu, Nov 13, 2008 at 10:30 AM, Farjola Zaloshnja <
>> farjola.zaloshnja_at_gmail.com> wrote:
>>
>>> Hi,
>>> I haven't used much the POST function in the jsr311 framework but if I
>>> were to post a list of strings will this be correct?
>>>
>>> On the client side:
>>>
>>>
>>> Client c = Client.*create*();
>>>
>>> URI url =
>>> *new* URI("getmylist");
>>>
>>> ClientRequest cr = ClientRequest.*create*().entity(List<String> mylist,
>>> "application/json").build(url, "POST");
>>>
>>> And the server side receiving will look like:
>>>
>>>
>>> @POST
>>> @Path("getmylist")
>>> *public* void getmylist(List<String> programids){ }
>>>
>>> Any comments ?
>>>
>>> Thank you,
>>>
>>> Farjola
>>>
>>>
>>>
>>>
>>
>>
>>
>