1

I am getting Json string from my server as below

var str = {"12:30 PM":"1:00 PM","11:30 AM":"12:00 PM"} 

I want to convert it in an array like

var str = [["12:30 PM","1:00 PM"], ["11:30 AM","12:00 PM"]];

How would I do that?

I tried to convert using jQuery.parseJSON(str), but it's giving error.

I also researched a lot in stackoverflow and there seems to be many solutionfor this problem but none of the solution is working for this issue.

3
  • Try var str = '{"12:30 PM":"1:00 PM","11:30 AM":"12:00 PM"}'; Commented Jan 13, 2014 at 3:54
  • Try this stackoverflow.com/questions/7611845/… Commented Jan 13, 2014 at 3:56
  • Never say "but it's giving error" without actually posting the error message. Commented Jan 13, 2014 at 4:04

5 Answers 5

2

The str is already an object.

You could use jQuery.map method.

var str = {"12:30 PM":"1:00 PM","11:30 AM":"12:00 PM"};
var result = $.map(str, function(value, key) {
  return [[key, value]];
});
Sign up to request clarification or add additional context in comments.

1 Comment

Didn't even notice the jquery tag :). Nice answer and doesn't require pollyfills for IE8 support (unless it's jQuery 2.x)
1

Try this map example

var str = {"12:30 PM":"1:00 PM","11:30 AM":"12:00 PM"};
var convert = Object.keys(str).map(function(k) {
    return [k, str[k]];
});

If you need support for IE <= 8, see Object.keys pollyfill and Array.prototype.map pollyfill

5 Comments

thanks for the response. I get error: "Uncaught TypeError: Object.keys called on non-object"
@user2584596 That shouldn't happen given the value of str in your question
I am using Chrome Version 31.0.1650.63 m, pretty latest.
I am using a jQuery plugin called Jquery.timepicker. here is the URL jonthornton.github.io/jquery-timepicker . This plugin require data in the format as I mentioned in my question. Your method works when I hardcode str = {"12:30 PM":"1:00 PM","11:30 AM":"12:00 PM"}; and then convert. However I am getting same data via Ajax call, so it's data coming from the Ajax which is not working. I copied data in str from console.log(data from ajax) so I am pretty sure it's same data
@user2584596 Then you need to show more code in your question as well as any error messages you see
0

Can you try this.

var p = {"12:30 PM":"1:00 PM","11:30 AM":"12:00 PM"} 
var o = [];

for (k in p) {
    if (p.hasOwnProperty(k)) {
        o.push([k, p[k]]);
    }
}

5 Comments

Gotta be careful with for..in loops. In some circumstances they can pick up more properties than you might expect
@Phil Can you please mention any instance?
Here's some more reading - jslinterrors.com/…
@Phil Does hasOwnProperty chekcing make any effect?
Yes, that's the recommended method of checking
0
var str = '{"12:30 PM":"1:00 PM","11:30 AM":"12:00 PM"}';
object = JSON.parse(str);
var my_array = new Array();

for(var key in object ){
    my_array[key] = object[key];
}

1 Comment

It's not working.. I am not sure if I am doing something wrong.
-1

Assuming you're talking about actual string that you're getting from the server, you can do a plain string replacement:

var str = '{"12:30 PM":"1:00 PM","11:30 AM":"12:00 PM"}';

str = str
.replace('{"','[["')
.replace('"}','"]]')
.replace('","','"],["')
.replace(/":"/g,'","')

This will make str into string representing an array. To make a real array out of it you can do something like this:

var arr;
eval('arr = ' + str);

This is the only legitimate use of eval I can advocate.

Demo: http://jsfiddle.net/h8c6w/

4 Comments

Instead of eval, you could use JSON.parse(str) and then map the object to a new array (see other answers)
thanks for your answer, unfortunately it gives error : Uncaught TypeError: Object [["12:30 PM","1:00 PM"],["11:30 AM","12:00 PM"]] has no method 'sort'
Would the downvoter please explain horrible vices of this answer? Perhaps by modifying working jsfiddle demo?
@user2584596 do not call str.sort(); in the example above - str is still a string. Instead call arr.sort(); - arr being a real array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.