Solved

Make an Object of String

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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)

726 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