saptarsi Mondal saptarsi Mondal - 1 month ago 7
jQuery Question

JSON string extract in Jquery

I have a Class Test and it have 2 String varaibles.

Class Test {

String id;
String priority;

Getters();
Setters();

}


I have converted my Class to JSON 'return Response.ok(test).build(); here test is object of Class Test'

Here, I am getting a list of test objects as:

[{"id":"INC0000001","priority":"High"},{"id":"INC0000001","priority":"Low"}


I need to extract the data in Jqery now. I am trying this:

$.each(json, function () {
$.each(this, function (name, value) {
console.log(name + '=' + value);
});
});


I need to print the values back to HTML and need to get those values from the list.

Answer

What exact HTML do you want to get? Here is an example of parsing JSON and formatting output HTML:

var json = [{"id":"INC0000001","priority":"High"},{"id":"INC0000001","priority":"Low"}];

var ul = $("<ul/>");

$.each(json, function () {
    var li = $("<li/>").text(this.id + ' has ' + this.priority + " priority");
    ul.append(li);  
});

$("body").append(ul);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>