如何通过javascript解析这个json数据

时间:2016-01-12 06:20:43

标签: javascript json

如何解析数据,我想要" CC"," CT"," TT"或者rsid,性别......这个数据是用javascript编写的?

谢谢大家〜

{
  "0": {
    "rrvalue": {
      "CC": 0.8652407470299768,
      "CT": 1.1161605636753882,
      "TT": 1.4398471271298607
    },
    "rsid": "rs505802",
    "sex": 0
  },
  "1": {
    "rrvalue": {
      "CC": 0.8655216050171408,
      "CT": 1.116522870472112,
      "TT": 1.4403145029090243
    },
    "rsid": "rs505802",
    "sex": 1
  }
}

3 个答案:

答案 0 :(得分:0)

var obj = {
  "0": {
    "rrvalue": {
      "CC": 0.8652407470299768,
      "CT": 1.1161605636753882,
      "TT": 1.4398471271298607
    },
    "rsid": "rs505802",
    "sex": 0
  },
  "1": {
    "rrvalue": {
      "CC": 0.8655216050171408,
      "CT": 1.116522870472112,
      "TT": 1.4403145029090243
    },
    "rsid": "rs505802",
    "sex": 1
  }
}

console.log( obj[ "0" ][ "rrvalue" ][ "CC" ] );
console.log( obj[ "0" ][ "rrvalue" ][ "CT" ] );
console.log( obj[ "0" ][ "rrvalue" ][ "TT" ] );

了解如何访问JSON / Object值here

答案 1 :(得分:0)

我认为这会对你有帮助。



var data = {
  "0": {
    "rrvalue": {
      "CC": 0.8652407470299768,
      "CT": 1.1161605636753882,
      "TT": 1.4398471271298607
    },
    "rsid": "rs505802",
    "sex": 0
  },
  "1": {
    "rrvalue": {
      "CC": 0.8655216050171408,
      "CT": 1.116522870472112,
      "TT": 1.4403145029090243
    },
    "rsid": "rs505802",
    "sex": 1
  }
}
/* Via jquery */
$.each(data,function(key,value){
  console.log("LOOP  -- >  " + key);
  $.each(data[key],function(k1,v1){
    if(k1 === "rrvalue"){
      $.each(data[key][k1],function(k2,v2){
        console.log(k2,v2);
      });
    } else {
      console.log(k1,v1);  
    }
  });
});


/* Via javascript */

for(var i in data){
  console.log("for Loop " + i);
  for(var j in data[i]){
    if(j === "rrvalue") {
      for(var k in data[i][j]){
          console.log(k + "    " +  data[i][j][k]);
      }
    } else {
      console.log(j + "   " + data[i][j]);
    }
  }
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:-2)

JSON.parse方法可以将json字符串转换为对象。见下面的例子,

var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';

var obj = JSON.parse(text)

现在obj将是一个json对象。看到它工作here ..

如需进一步阅读,请参阅mozila文档here