简体   繁体   English

如何在javascript中使用json值或在json对象中转换简单字符串?

[英]how to use json Value in javascript or convert simple string in json object?

var data = [
  {label: "a Organinzation",data: 1},
  {label: "b pvt Ltd.",data: 2},
  {label: "d Organization",data: 10},
  {label: "Completed Organization",data: 15},
  {label: "Running Organization",data: 34}
];

I have one piechart of jqplot in my jsp page and that takes the data with the above format..now my problem is its worked fine with static data. 我的jsp页面中有一个jqplot的图表,它使用上述格式的数据。现在,我的问题是它在静态数据下可以正常工作。 But when I want to include some dynamic data. 但是当我想包含一些动态数据时。 Its shows blank chart because of this data variable cant parse the string am providing... 由于该数据变量无法解析所提供的字符串,因此显示空白图表。
Am generating the same string as specified in above code snippet with the following 正在生成与上述代码段中指定的字符串相同的字符串,并包含以下内容

var total = rawdata.split(";");
var txt = null;

for (var i = 1; i < total.length - 1; i++) {
    if (i == 1) {
        txt = "{ label: \"" + total[i] + "\", data: " + total[i + 1] + "}";
    } else {
        txt = txt + ",  { label: \"" + total[i] + "\", data: " + total[i + 1] + "}";
    }
    i++;
}
alert(txt);

elem = $('#fl_3');

var data = [JSON.parse(txt)];

when i alert the txt varibale its giving me following pattern which is same as the code snippet i gave at first... 当我提醒txt变量时,它给了我以下模式,这与我最初给出的代码段相同...

{ label: "Abc LTd.", data: 42},
{ label: "A org", data: 2},  
{ label: "B Org", data: 6},  
{ label: "c Org", data: 1},  
{ label: "dbc comp ltd", data: 1},  
{ label: "avc comp pvt. ltd", data: 1}

Then why it cant parse it as json? 那为什么不能将其解析为json? In my browser, I got this error: 在我的浏览器中,出现以下错误:

Error: SyntaxError: JSON.parse: expected property name or '}'

If I write data=[txt]; 如果我写data=[txt]; , nothing appears. ,什么也没有出现。
If I write data = [JSON.parse(txt)]; 如果我写data = [JSON.parse(txt)]; then I get the error.. 然后我得到了错误。

Can anybody please help me? 有人可以帮我吗? How can I make this run? 我该如何运行?

Wrap the txt string within the brackets txt字符串包装在括号内

var data = JSON.parse('[' + txt + ']');

this will give you the array of objects. 这将为您提供对象数组。

You must also wrap the labels in double quotes as @guypursey mentioned, see JSFiddle 您还必须将标签用双引号引起来,如@guypursey所述,请参阅JSFiddle。

var txt = '{ "label": "Abc LTd.", "data": 42},\
{ "label": "A org", "data": 2},\
{ "label": "B Org", "data": 6},\
{ "label": "c Org", "data": 1},\
{ "label": "dbc comp ltd", "data": 1},\
{ "label": "avc comp pvt. ltd", "data": 1}';

var data = JSON.parse('[' + txt + ']');
console.log(data);

Why are building a string at all and then parse it back to object if you already have your data in array? 为什么要先构建一个字符串,然后将其解析回对象(如果您已将数据放入数组中)? Maybe you just need to reformat total somehow. 也许你只需要重新格式化total莫名其妙。 Not sure about structure of the total array. 不确定total组的结构。 Play with this: 玩这个:

var data = [];
for (var i = 0; i < total.length - 1; i = i + 2) {
    data.push({label: total[i], data: total[i + 1]});
}

Note: I'm assuming here that rawdata looks like this string: 注意:我在这里假设rawdata看起来像这样的字符串:

"a Organinzation;1;b pvt Ltd.;2;Completed Organization;3";

Try wrapping the property names in double quotes, as specified in the JSON standard . 尝试将属性名称包装在JSON标准中指定的双引号中。 For example: 例如:

txt= '{ "label": "' + total[i] + '", "data": ' + total[i+1] + '}';

I've got round having to use backslash escapes for the double quotation marks here by using single quotation marks to wrap the parts of the string that you're concatenating. 我不得不使用单引号将要连接的字符串部分包装起来,因此必须在双引号中使用反斜杠转义。 Just don't forget to escape any single quotation marks as a result. 只是不要忘了转义所有单引号。

And also don't forget to put single quotation marks around the square brackets for your data array and include them in the parse! 并且也不要忘记在数据数组的方括号周围加上单引号并将其包括在解析中!

var data = JSON.parse('[' + txt +']');

thanks for yr replies..but my problem solved with the following 感谢您的答复..但我的问题解决了以下

var data = eval(txt);

I didnt know..its just needed this only...thanks for your helps.. and please correct me if m wrong in this 我不知道..它只需要这个...谢谢您的帮助..如果这是错的,请纠正我

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

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