Solved

Make an Object of String

Posted on 2013-12-04
5
304 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now