0
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. 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...

{ 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? In my browser, I got this error:

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

If I write data=[txt];, nothing appears.
If I write data = [JSON.parse(txt)]; then I get the error..

Can anybody please help me? How can I make this run?

3
  • i think you only need to parse your 'data' object as JSON.parse(data); Commented Feb 15, 2013 at 9:29
  • i did that..but doesn't work Commented Feb 15, 2013 at 9:32
  • I posted an answer below in case you haven't tried that. But I think the question could be clearer if you show us an example of what rawdata looks like. So far I can only see the output you want in data but it would be good to see what it is you start with. Commented Feb 15, 2013 at 9:44

4 Answers 4

3

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. Not sure about structure of the total array. 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:

"a Organinzation;1;b pvt Ltd.;2;Completed Organization;3";
Sign up to request clarification or add additional context in comments.

Comments

3

Wrap the txt string within the brackets

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

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);

Comments

2

Try wrapping the property names in double quotes, as specified in the JSON standard. 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 +']');

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.