Solved

Make an Object of String

Posted on 2013-12-04
5
309 Views
Last Modified: 2013-12-10
I have a String looking something like this:
{'0':{'label':52,'data':[0,45065]},'1':{'label':51,'data':[1,54935]}'0':{'label':53,'data':[0,1133]},'1':{'label':60,'data':[1,9513]},'2':{'label':56,'data':[2,12586]},'3':{'label':63,'data':[3,10720]},'4':{'label':62,'data':[4,11879]},'5':{'label':54,'data':[5,11767]},'6':{'label':57,'data':[6,13861]},'7':{'label':61,'data':[7,10292]},'8':{'label':58,'data':[8,6325]},'9':{'label':59,'data':[9,1581]},'10':{'label':55,'data':[10,43]}}";

How do I make it to a list of objects (or whatever it is called). When I use alert on it, I want it to look like this when I use alert() on it.:
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
0
Comment
Question by:mdoland
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39695641
[1,54935]}'0':{

there's a missing coma just before the 0
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 39695672
Test page : http://jsfiddle.net/8cGhL/

var jsonString = "{'0':{'label':52,'data':[0,45065]},'1':{'label':51,'data':[1,54935]}'0':{'label':53,'data':[0,1133]},'1':{'label':60,'data':[1,9513]},'2':{'label':56,'data':[2,12586]},'3':{'label':63,'data':[3,10720]},'4':{'label':62,'data':[4,11879]},'5':{'label':54,'data':[5,11767]},'6':{'label':57,'data':[6,13861]},'7':{'label':61,'data':[7,10292]},'8':{'label':58,'data':[8,6325]},'9':{'label':59,'data':[9,1581]},'10':{'label':55,'data':[10,43]}}";
var trueJSONString = jsonString
    .replace("}'0","},'0")
    .replace(/'/g, "\"");

var objects = JSON.parse(trueJSONString);

alert( objects );
alert(objects[2].data[1]); // 12586
alert(objects[9].data[1]); // 1581

Open in new window

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39695831
I think the proper way is like this: http://jsfiddle.net/DT3CK/

var json = {
    '0': {
        'label': 52,
        'data': [0, 45065]
    },
    '1': {
        'label': 51,
        'data': [1, 54935]
    },
    '0': {
        'label': 53,
        'data': [0, 1133]
    },
    '1': {
        'label': 60,
        'data': [1, 9513]
    },
    '2': {
        'label': 56,
        'data': [2, 12586]
    },
    '3': {
        'label': 63,
        'data': [3, 10720]
    },
    '4': {
        'label': 62,
        'data': [4, 11879]
    },
    '5': {
        'label': 54,
        'data': [5, 11767]
    },
    '6': {
        'label': 57,
        'data': [6, 13861]
    },
    '7': {
        'label': 61,
        'data': [7, 10292]
    },
    '8': {
        'label': 58,
        'data': [8, 6325]
    },
    '9': {
        'label': 59,
        'data': [9, 1581]
    },
    '10': {
        'label': 55,
        'data': [10, 43]
    }
};

function convertToArray(){
    var result = [];
    for(item in json){
        result.push(json[item]);
    }
    return result;
}

alert(convertToArray());

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 39695835
@AlexCode :

I have a String looking something like this
0
 
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 250 total points
ID: 39695871
Ok, if it's a string we need to work it out a bit: http://jsfiddle.net/DT3CK/2/

Valid JSON requires " not '

var json = JSON.parse("{'0':{'label':52,'data':[0,45065]},'1':{'label':51,'data':[1,54935]},'0':{'label':53,'data':[0,1133]},'1':{'label':60,'data':[1,9513]},'2':{'label':56,'data':[2,12586]},'3':{'label':63,'data':[3,10720]},'4':{'label':62,'data':[4,11879]},'5':{'label':54,'data':[5,11767]},'6':{'label':57,'data':[6,13861]},'7':{'label':61,'data':[7,10292]},'8':{'label':58,'data':[8,6325]},'9':{'label':59,'data':[9,1581]},'10':{'label':55,'data':[10,43]}}".replace(/'/g, "\""));

function convertToArray() {
    var result = [];
    for (item in json) {
        result.push(json[item]);
    }
    return result;
}

alert(convertToArray());

Open in new window

0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ajax jquery 3 25
Insert Button on a table 16 38
Html input text color the frame line 2 22
JQuery Search Filter 2 39
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question