简体   繁体   English

在咖啡脚本中解析json

[英]parse json in coffee script

I Have this JSON data. 我有此JSON数据。 I want to parse it using CoffeeScript. 我想使用CoffeeScript解析它。 How to access the value of answer? 如何获得答案的价值?

{"options": [{"answer": "Description for xss injection is", "answer_options": 0, "answer_lang": null}]}

I have tried this method but failed: 我尝试过这种方法,但是失败了:

for key,value of data
    for k,v in value
          res.send "#{v}"

I tried hard and got this solution. 我努力尝试并获得了此解决方案。 And its worikng :D 和它的工作:D

body={"options": [{"answer": "Description for xss injection is","answer_options":0, "answer_lang": null}]}
data=JSON.parse(body);
for i of  data.options
      counter = data.options[i];
      res.send "#{i} #{counter.answer}";

Hope, it helps someone :D 希望对别人有帮助:D

Assuming the json object is stored in a variable called "object": 假设json对象存储在一个名为“ object”的变量中:

answer = object["options"][0]["answer"] //answer = "Description for xss injection is"

another way to access: 另一种访问方式:

answer = object.options[0].answer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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