1

I have this json

"urls": {
 "title1": {
 "url": "someurl1"
 },
 "title12": {
 "url": "someurl2"
 }
 }

I want get title and value of url out in an element for each. Something like this

$.each(value.urls, function (key, value) {
 $('.log-list-js').append('<a href="'+ title +'">'+ someurl +'</a>');
});

Hope it makes sense.

Can you help me ?

asked Nov 18, 2013 at 12:56

3 Answers 3

2

Try this

 var values = {
 "urls": {
 "title1": {
 "url": "someurl1"
 },
 "title12": {
 "url": "someurl2"
 }
 }
 }
 for (var key in values.urls) {
 $('.log-list-js').append('<a href="' + values.urls[key].url + '">' + values.urls[key].url + '</a>');
 }

DEMO

answered Nov 18, 2013 at 12:59
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but what about the title? That should be the text of each element?
I found out 'key' : )
0
$('.log-list-js').append('<a href="' + value.urls[key].url + '">' + value.urls[key].url + '</a>');

This is format

answered Nov 18, 2013 at 13:01

Comments

0

Your json should be converted to object first, use JSON.parse method if you are having a json string. object should something like this

urls = {
 "title1": {
 "url": "someurl1"
 },
 "title12": {
 "url": "someurl2"
 }
 };

then your method is correct with some corrections to the code

$.each(urls, function(index, value){
 $('.log-list-js').append('<a href="'+ index +'">'+ value.url +'</a>');
});

here is the demo on jsfiddle

answered Nov 18, 2013 at 13:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.