Jquery中的JSON字符串提取

时间:2016-10-28 07:10:35

标签: jquery arrays json

我有一个班级Test,它有两个字符串变量。

Class Test {
    String id;
    String priority;

     Getters();
     Setters();
}

我已将我的类转换为JSON'返回Response.ok(test).build();这里test是类Test

的对象

在这里,我得到一个测试对象列表:

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

我现在需要在jQuery中提取数据。我正在尝试这个:

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

我需要将值打印回HTML,并且需要从列表中获取这些值。

3 个答案:

答案 0 :(得分:0)

您想要获得哪些HTML?以下是解析JSON和格式化输出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>

答案 1 :(得分:0)

您可以尝试这样

$.each(json, function (index, value) {
    console.log('id =' + value.id +', priority ='+ value.priority );
});

答案 2 :(得分:0)

检查我已添加,及其工作

&#13;
&#13;
var json = [{"id":"INC0000001","priority":"HIGH"},{"id":"INC0000001","priority":"HIGH"},{"id":"INC0000002","priority":"CRITICAL"}];
    var countPriority_ecomm_high = 0;
    var countPriority_ecomm_low = 0;
    var countPriority_ecomm_medium = 0;
    var countPriority_ecomm_critical = 0;
$.each(json, function( key, value ) {       
    if (value.priority == "HIGH"){
        countPriority_ecomm_high++;
    }

    if (value.priority == "MEDIUM"){
        countPriority_ecomm_medium++;
    }

    if (value.priority == "LOW"){
        countPriority_ecomm_low++;
    }

    if (value.priority == "CRITICAL"){
        countPriority_ecomm_critical++;
    }
     
    })
 alert('HIGH ='+ countPriority_ecomm_high +', LOW ='+ countPriority_ecomm_low+', MEDIUM ='+countPriority_ecomm_medium+', CRITICAL ='+ countPriority_ecomm_critical)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;