We help IT Professionals succeed at work.

How convert JSON string to multidimensional array?

NiklasMoller
NiklasMoller asked
on
16,291 Views
Last Modified: 2013-11-11
I'm just starting to learn this JSON-stuff, but I cant get it to work.
I am using the JSON class from here: http://www.json.org/json.as

I have a php-script that collects some info on a given month of the year, stores that as a multidimensional array, then converts that array to a JSON object using the built in php function json_encode. (info here: http://usphp.com/manual/en/function.json-decode.php)
Then I echo that string. This seems to work. If I just look at the echoed output in a browser it reads:

-----------
{"day1":{"weekday_number":"1","name_abbr":"Mon","name_full":"Monday"},"total_days":"30","day2":{"name_abbr":"Tue","name_full":"Tuesday","weekday_number":2},"day3":{"name_abbr":"Wed","name_full":"Wednesday","weekday_number":3},"day4":{"name_abbr":"Thu","name_full":"Thursday","weekday_number":4},"day5":{"name_abbr":"Fri","name_full":"Friday","weekday_number":5},"day6":{"name_abbr":"Sat","name_full":"Saturday","weekday_number":6},"day7":{"name_abbr":"Sun","name_full":"Sunday","weekday_number":0},"day8":{"name_abbr":"Mon","name_full":"Monday","weekday_number":1},"day9":{"name_abbr":"Tue","name_full":"Tuesday","weekday_number":2},"day10":{"name_abbr":"Wed","name_full":"Wednesday","weekday_number":3},"day11":{"name_abbr":"Thu","name_full":"Thursday","weekday_number":4},"day12":{"name_abbr":"Fri","name_full":"Friday","weekday_number":5},"day13":{"name_abbr":"Sat","name_full":"Saturday","weekday_number":6},"day14":{"name_abbr":"Sun","name_full":"Sunday","weekday_number":0},"day15":{"name_abbr":"Mon","name_full":"Monday","weekday_number":1},"day16":{"name_abbr":"Tue","name_full":"Tuesday","weekday_number":2},"day17":{"name_abbr":"Wed","name_full":"Wednesday","weekday_number":3},"day18":{"name_abbr":"Thu","name_full":"Thursday","weekday_number":4},"day19":{"name_abbr":"Fri","name_full":"Friday","weekday_number":5},"day20":{"name_abbr":"Sat","name_full":"Saturday","weekday_number":6},"day21":{"name_abbr":"Sun","name_full":"Sunday","weekday_number":0},"day22":{"name_abbr":"Mon","name_full":"Monday","weekday_number":1},"day23":{"name_abbr":"Tue","name_full":"Tuesday","weekday_number":2},"day24":{"name_abbr":"Wed","name_full":"Wednesday","weekday_number":3},"day25":{"name_abbr":"Thu","name_full":"Thursday","weekday_number":4},"day26":{"name_abbr":"Fri","name_full":"Friday","weekday_number":5},"day27":{"name_abbr":"Sat","name_full":"Saturday","weekday_number":6},"day28":{"name_abbr":"Sun","name_full":"Sunday","weekday_number":0},"day29":{"name_abbr":"Mon","name_full":"Monday","weekday_number":1},"day30":{"name_abbr":"Tue","name_full":"Tuesday","weekday_number":2}}
-----------
It seems to be formatted okay.

This string is what I want to load into flash, and then convert it to a multidimensional array again in flash.

So I have this code in actionscript (see attached)
I do load the string, I know that much, because trace(jsonString) outputs a string.
However, all the quotes are escaped with backslashes. So the trace óf the loaded string looks like this:
-----------
{"{\"day1\":{\"weekday_number\":\"1\",\"name_abbr\":\"Mon\",\"name_full\":\"Monday\"},\"total_days\":\"30\",\"day2\":{\"name_abbr\":\"Tue\",\"name_full\":\"Tuesday\",\"weekday_number\":2},\"day3\":{\"name_abbr\":\"Wed\",\"name_full\":\"Wednesday\",\"weekday_number\":3  ........ etc etc
---------
So my questions:
- why are the quotes escaped with backslashes? Does this matter when I try to convert the string to an object? Why is this happening?
- I want to make this string into an array in flash... how do I do it? and how do I access the items in the array once it has been converted to an array?

var jsondata:LoadVars = new LoadVars();
var JSON:JSON = new JSON();
 
jsondata.onLoad = function() {
	var jsonString:String = JSON.stringify(this);
	trace(jsonString); //this works, but the output shows a string where every " has been escaped like so: \"
	var jsonObject:Object = JSON.parse(jsonString);
	trace (jsonObject);//this outputs [object Object], so it seems some kind of object has been created, but how do I know, and how do I access the items?
};
 
jsondata.load($php_filepath);

Open in new window

Comment
Watch Question

Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
Sounds like you should read the JSON in you actionscript directly

I found this
http://www.json.org/json.as
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:

Author

Commented:
>Sounds like you should read the JSON in you actionscript directly

I dont understand what you mean... Can you suggest specifically what I am doing wrong, or what I should do differently?

Thanks for taking the time!
cheers
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
I think I do not understand the issue.

If you read the JSON inside the action script and parse it, then the JSON content should be available to you directly in name/value pairs - no need to do anything with the json to create arrays or such...

Author

Commented:
Ok. thanks for taking the time anyway.
I think I am getting closer to understanding the issue by myself, I will post anything here if I manage to solve the problem...
If anybody else has a working solution to what I am trying to do, please suggest it! :)
thanks
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
>stringify would escape al quotes so why do it if you do not want that?

Hehe. Good point! :)
I will rearrange my code when I get back from work, and I'll see if I have any luck with your suggestion.
Thanks!
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
Great - feel free to accept your own comment
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.