?
Solved

Make an Object of String

Posted on 2013-12-04
5
Medium Priority
?
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 1000 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 To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

801 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