Solved

Make an Object of String

Posted on 2013-12-04
5
280 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
Comment Utility
[1,54935]}'0':{

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

Accepted Solution

by:
leakim971 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

763 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

12 Experts available now in Live!

Get 1:1 Help Now