简体   繁体   English

将json对象打印为字符串格式

[英]print json object to string format

Iam trying print json object to string format. 我正在尝试将json对象打印为字符串格式。

(Actually i got these data value from ruby code var data = '<%=[@pro {|c| {x:cx, y:cy}}].to_json%>' ) (实际上,我是从ruby代码中获取这些数据值的var data = '<%=[@pro {|c| {x:cx, y:cy}}].to_json%>' ))

while printing the data variable i got values containing &quot instead of ' 在打印数据变量时,我得到的值包含&quot而不是'

var data=[[{&quot;x&quot;:1,&quot;y&quot;:0}]]

I want to get the values in the format var data="[[{ 'x': '1', 'y': '0' }]]"; 我想获取格式为var data="[[{ 'x': '1', 'y': '0' }]]";

while removing &quot; 同时删除&quot; using the code var dataset=JSON.parse(data.replace(/&quot;/g,'"')); got in the following format. 使用代码var dataset=JSON.parse(data.replace(/&quot;/g,'"'));获得以下格式。

var dataset =[[{"x":1,"y":0}]] var数据集= [[{“ x”:1,“ y”:0}]]

i want to print the values like [[{ 'x': '1', 'y': '0' }]] .How it is possible. 我想打印类似[[{ 'x': '1', 'y': '0' }]]值。

If you are using only 2 variables, than you can simple do like that: 如果仅使用两个变量,则可以简单地执行以下操作:

var data = [[{'x': '<%= @pro.x %>', 'y': '<%= @pro.y %>'}]];  

And no need to use to_json method. 无需使用to_json方法。

If you are want to stringify many values than this code works like a charm: 如果您想对许多值进行字符串化处理,则此代码就像一个超级魅力:

<% array = [[{'x' => 1, 'y' => 1}], [{'x' => 2, 'y' => 2}], [{'x' => 3, 'y' => 3}]] %>
<%= array.to_json %>

And he outputs: 他输出:

[[{"x":1,"y":1}],[{"x":2,"y":2}],[{"x":3,"y":3}]]

If you need to send json by request in your controller use: 如果您需要通过控制器中的请求发送json,请使用:

def send_json_array
    array = [[{'x' => 1, 'y' => 1}], [{'x' => 2, 'y' => 2}], [{'x' => 3, 'y' => 3}]]
    response.headers['Content-type'] = "text/plain; charset=utf-8"
    render :text => array.to_json
end

JSON.stringify converts an object to its json format. JSON.stringify将对象转换为其json格式。

var dataset =[[{"x":1,"y":0}]]
var JsonData = JSON.stringify(dataset);

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

相关问题 如何在javascript中将JSON字符串对象打印为下拉列表 - How to print JSON String object as a dropdown in javascript 来自String的JSON对象的Polymer打印部分 - Polymer print part of a JSON object from String JavaScript对象(JSON)到URL字符串格式 - JavaScript Object (JSON) to URL String Format 将JSON格式的字符串转换为Angular中的真实对象 - Converting JSON format string into a real object in Angular 如何在 JS 中以人类可读的格式打印 JSON 字符串 - How to print JSON String in human readable format in JS Thymeleaf将JSON字符串作为JSON对象打印到javascript变量中 - Thymeleaf print JSON string as JSON object into a javascript variable 将JS对象字符串格式化/隐藏为有效的JSON格式 - Format/covert JS object string to valid JSON format 在Angular中将JSON格式的字符串数组转换为真实的JSON数组对象 - convert JSON format string array into real JSON array object in Angular JS, JSON - 转换一个 json object 在屏幕上以字符串格式显示并连接 - JS, JSON - Coverter a json object to show in string format on the screen and concatenated 从 python object 使用 Z319C3206A7F10C17C3B9116D4A95764 打印 JSON 字符串 - print JSON string converted from python object using flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM