如何从这个json字符串中读取数据?

时间:2017-04-05 08:26:35

标签: javascript c# angularjs json

[
   {
      "Intent":"what is manufacturer name?",
      "Entity":"Name",
      "Response":"Test",
      "Status":"0",
      "Created_Date":"2017-04-04T00:00:00",
      "Response_Count":0,
      "Response_Count_string":0
   },
   {
      "Intent":"hi",
      "Entity":"hi",
      "Response":"hiiii",
      "Status":"0",
      "Created_Date":"2017-03-28T10:22:00",
      "Response_Count":0,
      "Response_Count_string":0
   },
   {
      "Intent":"how are you?",
      "Entity":"are you fine",
      "Response":"good!cool",
      "Status":"1",
      "Created_Date":"2017-03-28T10:22:38",
      "Response_Count":0,
      "Response_Count_string":0
   }
]

4 个答案:

答案 0 :(得分:4)

您的data是字符串化的JSON。你应该只能JSON.parse它。

像这样:

var data = "[{\"Intent\":\"what is manufacturer name?\",\"Entity\":\"Name\",\"Response\":\"Test\",\"Status\":\"0\",\"Created_Date\":\"2017-04-04T00:00:00\",\"Response_Count\":0,\"Response_Count_string\":0},{\"Intent\":\"hi\",\"Entity\":\"hi\",\"Response\":\"hiiii\",\"Status\":\"0\",\"Created_Date\":\"2017-03-28T10:22:00\",\"Response_Count\":0,\"Response_Count_string\":0},{\"Intent\":\"how are you?\",\"Entity\":\"are you fine\",\"Response\":\"good!cool\",\"Status\":\"1\",\"Created_Date\":\"2017-03-28T10:22:38\",\"Response_Count\":0,\"Response_Count_string\":0}]"

var jsondata = JSON.parse(data)
console.log(jsondata[0].Intent)
console.log(jsondata[1].Intent)
console.log(jsondata[2].Intent)

答案 1 :(得分:0)



<!DOCTYPE html>
<html>
<body>
 
<p id="demo"> </p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var str = "[{\"Intent\":\"what is manufacturer name?\",\"Entity\":\"Name\",\"Response\":\"Test\",\"Status\":\"0\",\"Created_Date\":\"2017-04-04T00:00:00\",\"Response_Count\":0,\"Response_Count_string\":0},{\"Intent\":\"hi\",\"Entity\":\"hi\",\"Response\":\"hiiii\",\"Status\":\"0\",\"Created_Date\":\"2017-03-28T10:22:00\",\"Response_Count\":0,\"Response_Count_string\":0},{\"Intent\":\"how are you?\",\"Entity\":\"are you fine\",\"Response\":\"good!cool\",\"Status\":\"1\",\"Created_Date\":\"2017-03-28T10:22:38\",\"Response_Count\":0,\"Response_Count_string\":0}]"; 
    var res = str.replace("\'", "");
    res=  JSON.parse(res)
    document.getElementById("demo").innerHTML = res[0].Intent;
}
</script>

</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

JSON字符串中读取数据的方法:

<强> 1。使用Javascript

&#13;
&#13;
var jsonStr = "[{\"Intent\":\"what is manufacturer name?\",\"Entity\":\"Name\",\"Response\":\"Test\",\"Status\":\"0\",\"Created_Date\":\"2017-04-04T00:00:00\",\"Response_Count\":0,\"Response_Count_string\":0},{\"Intent\":\"hi\",\"Entity\":\"hi\",\"Response\":\"hiiii\",\"Status\":\"0\",\"Created_Date\":\"2017-03-28T10:22:00\",\"Response_Count\":0,\"Response_Count_string\":0},{\"Intent\":\"how are you?\",\"Entity\":\"are you fine\",\"Response\":\"good!cool\",\"Status\":\"1\",\"Created_Date\":\"2017-03-28T10:22:38\",\"Response_Count\":0,\"Response_Count_string\":0}]";

var jsonObj = JSON.parse(jsonStr);

for (var i in jsonObj) {
  console.log(jsonObj[i].Intent);
}
&#13;
&#13;
&#13;

<强> 2。使用AngularJS

&#13;
&#13;
var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl',function($scope) {
    var jsonStr = "[{\"Intent\":\"what is manufacturer name?\",\"Entity\":\"Name\",\"Response\":\"Test\",\"Status\":\"0\",\"Created_Date\":\"2017-04-04T00:00:00\",\"Response_Count\":0,\"Response_Count_string\":0},{\"Intent\":\"hi\",\"Entity\":\"hi\",\"Response\":\"hiiii\",\"Status\":\"0\",\"Created_Date\":\"2017-03-28T10:22:00\",\"Response_Count\":0,\"Response_Count_string\":0},{\"Intent\":\"how are you?\",\"Entity\":\"are you fine\",\"Response\":\"good!cool\",\"Status\":\"1\",\"Created_Date\":\"2017-03-28T10:22:38\",\"Response_Count\":0,\"Response_Count_string\":0}]";

    $scope.jsonObj = JSON.parse(jsonStr);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <div ng-repeat="item in jsonObj">
    {{item.Intent}}
  </div>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

您可以尝试类似

的内容
{{1}}