2017-10-06 64 views
0

我得到这种类型的JSON格式从我的查询现在我想用 价值得到最高价值如何从JSON格式得到它:如何从JSON编码功能

Json format I am Getting : 

{"p":{"r":true,"rows":[{"id":"1","gop":"Contents","top":"p is simply dummy ","no":"1"}]}} 

干什么米样反应:

success:function(data){ 
      var obj=$.parseJSON(data); 
      var privacyss=''; 
if(obj.p.res){ 
var privacyss='<p>'+p.rows.top+'</p>'; 
$("#Privacys").html(privacyss); 
} 
} 

,但是我不能够获得的价值通过p.rows.top ..所以,请让我知道 如何获得价值

+1

你必须申请结果中,如果(OBJ .P。 res){这个条件你确定在你的p数组中存在res键吗? –

回答

1

试试这个代码:

obj.p.rows[0].top 
+0

Thanx它为我工作usman ikram @ – Ashwini

0

用这种方式

var privacyss ='

'+ p.rows [0] .top'+'

';

0

var data={"p":{"r":true,"rows":[{"id":"1","gop":"Contents","top":"p is simply dummy ","no":"1"}]}}; 
 

 
// gives entire object 
 
console.log(data.p.rows[0]); 
 

 
// access individual key 
 
console.log(data.p.rows[0].top);

0
  1. rows是对象数组。所以你应该在rows之后使用index来获取。在您的代码中将此obj.p.res更改为obj.p.rows[0].top
  2. 其次是您使用p.res,作为回应没有资源,而使用rows

请参阅以下代码以供参考。

var response = '{"p":{"r":true,"rows":[{"id":"1","gop":"Contents","top":"p is simply dummy ","no":"1"}]}}'; 
 

 
var resParsed = JSON.parse(response); 
 
console.log(resParsed.p.rows[0].top);

0

简单,你可以通过

obj.p.rows[0].top 

叫它但如果行有,所以不要用指标约束多个索引使用的每个或相关地图

如:

{"r":true,"rows":[{"id":"1","gop":"Contents","top":"p is simply dummy ","no":"1"}, {"id":"2","gop":"Contents","top":"p is other simply dummy ","no":"2"}]}} 

做这样

success:function(data){ 
    var obj = JSON.parse(data); 
    if(obj.p.res){ 
     $.each(obj.p.rows, function (i, row) { 
      var privacyss='<p>'+row.top+'</p>'; 
      $("#Privacys").append(privacyss); // you can call html() too but it will overrite html 
     }); 
    } 
} 


建议

,如果你正在处理JSON然后用dataType : 'JSON'

url : url, 
dataType : 'JSON', // add this field 
success:function(data){ 
    // var obj = JSON.parse(data); you don't need to parse it 
    data.p.res // can access attribute 
}