0

This is my test.json file :

[{"id":"12","url":"http:\/\/localhost\/test\/1\/images\/pic\/3.jpg"},
{"id":"11","url":"http:\/\/localhost\/test\/1\/images\/pic\/1.png"},
{"id":"10","url":"http:\/\/localhost\/test\/1\/images\/pic\/2.png"}]

I want to print it on emphasized text using jQuery.

<div>12<img src="http://localhost/test/1/images/pic/3jpg" /></div>
<div>11<img src="http://localhost/test/1/images/pic/1jpg" /></div>
<div>10<img src="http://localhost/test/1/images/pic/2jpg" /></div>
3
  • 2
    This is not a valid JSON. Commented Nov 24, 2015 at 23:52
  • Please, change the tittle of the question for another one which give people a best idea about what do you need. "Need help in java/c++/maven/linux/jason" doesn't give further information that the labels "java, c++, maven, linux, or jason". Commented Nov 24, 2015 at 23:58
  • I'd go with the classic formula: What did you try? What didn't work? It will be easier to answer you then. Commented Nov 25, 2015 at 0:07

2 Answers 2

1

If you want to get your json from the server rather than hard-coding it into your javascript, use jquery's .getJSON() method (see docs). Then iterate over the response data using the .each() method (see docs).

$.getJSON(
    'test.json',
    function(data) {
        $.each(data, function(i, obj) {
            el = $('<div>' + obj['id'] + '<img src="'+ obj['url'] +'" /></div>');
            $('body').append(el);
        });
    }
);
Sign up to request clarification or add additional context in comments.

Comments

1

Check the valid JSON bellow and try to reformat yours like it, you can find also example that show you how you can parse a JSON Object using for in to acheive the desired result.

Hope this helps.

var test_json = 
[
   {"id":"12","url":"http:\/\/localhost\/test\/1\/images\/pic\/3.jpg"},
   {"id":"11","url":"http:\/\/localhost\/test\/1\/images\/pic\/1.png"},
   {"id":"10","url":"http:\/\/localhost\/test\/1\/images\/pic\/2.png"}
];

for ( var key in test_json )
{
    console.log('<div>'+test_json[key].id+'<img src="'+test_json[key].url+'" /></div>');
}

Comments

Your Answer

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.