We help IT Professionals succeed at work.

Ajax/Json decode for multi-dimension arrays using JQuery/JS

brothertom
brothertom asked
on
1,266 Views
Last Modified: 2013-11-08
I am trying to get my head around multi-dimension arrays in JQuery/JS.
From the returned JSON string (below) returned from an Ajax from JS, I need to be able to:

1. Store the top level value (e.g. 12345)
2. Then iterate through the remaining key/value pairs

So, if a simple output was required, I need:
12345
name1:value1
name2:value2
name3:value3

98765
name1:value1a
name2:value2a
name3:value3a

I have tried using the JSON.parse function but to no avail.
Could someone show me a worked example utilising JQuery/JS from the description above?

Many thanks
BT



The following array when encoded into JSON :
$arr['12345'] = array('name1' => 'value1', 'name2' => 'value2', 'name3' => 'value3');
$arr['98765'] = array('name1' => 'value1a', 'name2' => 'value2a', 'name3' => 'value3a');

gives output of
{"12345":{"name1":"value1","name2":"value2","name3":"value3"},"98765":{"name1":"value1a","name2":"value2a","name3":"value3a"}}

Open in new window

Comment
Watch Question

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
I don't understand what is your input json string ?

this : {"12345":{"name1":"value1","name2":"value2","name3":"value3"},"98765":{"name1":"value1a","name2":"value2a","name3":"value3a"}}
?

and you want to output :

12345
name1:value1
name2:value2
name3:value3

98765
name1:value1a
name2:value2a
name3:value3a

Author

Commented:
correct
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hello @brothertom,

Did you try the code ? It work fine for me.

Kind Regards.

Author

Commented:
Yes, I used code as a basis for some specific refinements.  Many thanks
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You're welcome! Thanks for your feedback and the points!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
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.