views:

54

answers:

2

I am trying to parse to JSON object in java. I am posting json key value object using dojo.xhrPost() method as shown below:

dojo.xhrPost({
url: /esturl/,
handleAs : "text",
content : {INST1 : {"0001" : "aa"},
            INST2 : {"0002" : "bb"},
            INST3 : {"0003" : "cc"}},
load : load(data),
error : error
});

Now I am reading this post data from request context:

Map<String, String[]> paramMap = requestContext.getParameterMap();

when I am printing this in loop:

Iterator it = paramMap.entrySet().iterator();
while(it.hasnext()){
 Map.entry pairs = (Map.Entry) it.next();
 System.out.println(pairs.getKey());
 System.out.println(pairs.getkValue());

}

this returns me :

INST1
[Ljava.lang.String;@1b4cef
INST2
[Ljava.lang.String;@5801d

like wise, but I should be getting values like

INST1 : {"0001" : "aa"}, INST2 : {"0002" : "bb"}, INST3 : {"0003" : "cc"}}, any guidance would be highly appreciated.

Update

When I try to get value of one parameter using

String[] X = requestContext.getParameters{"INST1"};

if (X != null){
   System.out.println(X.length);
   System.out.println(X[0]);
   System.out.println(X[0].length);
}

then am getting :

1
[object Object]
[object Object]

Q Now how can I get actual values of Object like INST1 : {"0001" : "aa"} instead of [object Object] ?

+1  A: 

as given in the dojo documentation here the object which you set up will be read as name1=value1. So may be in your case the variables are being passed like INST1=0001,INST1=aa.

You can try to make the syntax of 'content' on these lines - INST1 : '{"0001" : "aa"}' or INST1 : "{\"0001\" : \"aa\"}" so that INST1 has 1 unambiguous value

Gaurav Saxena
Yes, now am able to get values as expected, I never thought that there would be such an issue, thanks Gaurav for your guidance.
Rachel
+1  A: 

you can use this modified JSONObject class, pass the raw JSON code through the constructor or create (new object and assign JSON content later..) and use built-in get methods. view full javadoc here.

Elad Karako