Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Why can't get the Json pair value

Avatar

Former Community Member

The server servlet code is below(return to client);

JSONWriter jw = new JSONWriter(response.getWriter()); try { jw.array(); jw.object(); jw.key("result").value(result); jw.endObject(); jw.endArray(); } catch (JSONException e) { log.error("JSON Exception:", e); }

And the client file JS in fold clientlibraryfold "xxx.js" not JSP page is below:

CQ.Ext.Ajax.request({ url: CQ.HTTP.externalize("/bin/customer"), method: "GET", success: function(response) { var info = response.responseText; alert(info);//[{"result":1}] var ack = info.result1;//undefined error alert(ack); }, failure: function(response) { } });

So how to get the Key "result" value? I did try to use couple of way but fail. Anybody know that? Please comment it, thanks a lot.

1 Accepted Solution

Avatar

Correct answer by
Level 10

We have a community article that shows how to work with JSONWriter - this is exactly what you are looking for.

http://helpx.adobe.com/experience-manager/using/using-jsonwriter-objects-display-cq.html

View solution in original post

6 Replies

Avatar

Employee

You need to parse the resposne text into a JavaScript object. Something like

var obj = CQ.Ext.util.JSON.decode(response.responseText); var ack = obj.result;

Avatar

Employee

bbboy2014 wrote...

It not work according to your way :(

 

Sorry, I misread your code. I missed that the JSON is an object wrapped in an array. I should have written

var ack = obj[0].result;

Avatar

Former Community Member

justin_at_adobe wrote...

bbboy2014 wrote...

It not work according to your way :(

 

Sorry, I misread your code. I missed that the JSON is an object wrapped in an array. I should have written

  1. var ack = obj[0].result;

 

 


Exactly

Avatar

Correct answer by
Level 10

We have a community article that shows how to work with JSONWriter - this is exactly what you are looking for.

http://helpx.adobe.com/experience-manager/using/using-jsonwriter-objects-display-cq.html