简体   繁体   中英

Converting java JSONObject to javascript

I have a JSONObject I'm sending through AJAX.

JSONObject obj = new JSONObject();
obj.put("nbLike", result);
obj.put("username", "bill");

Then in the success function I want to access the obj properties . eg : username

I'have tried with JSON.parse(obj) but I got an error : Uncaught SyntaxError: Unexpected token '

server side the log shows : {"username":"bill","nbLike":1} //log.info(obj)

client side the log show : ['username':'bill', 'nbLike':1] //console.log(result)

I'd like something like :

console.log("Username : " + obj.username)

Thank you

Convert JSONObject to String before sending it to frontend and add "application/json" to Content-type of your response. Then you can use it as simple js object, don't need to convert it.

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