解析JSON字符串并获取键值

时间:2017-05-17 12:47:52

标签: javascript jquery json

我有以下JSON

[{ "GameID":"1109100", "StartTime": "4:00 PM", "EndTime": "1/1/1900 5:30:00 PM", "Duration": "90","TeamID":"101567"},{ "GameID":"1108768", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"109873"},{ "GameID":"1108776", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"117674"},{ "GameID":"1108742", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24621"},{ "GameID":"1108751", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24622"},{ "GameID":"1108757", "StartTime": "10:30 AM", "EndTime": "1/1/1900 12:00:00 PM", "Duration": "90","TeamID":"24623"},{ "GameID":"1109098", "StartTime": "2:30 PM", "EndTime": "1/1/1900 4:00:00 PM", "Duration": "90","TeamID":"24746"}] 

我希望使用TeamID获取游戏ID,开始时间和结束时间值。我怎么能这样做?

编辑:从评论中解析JSON的实际功能

function doParseTime(msg){ 
    var data = $.parseJSON(msg); 
    $.each(data, function() { 
        $.each(this, function(key, val){ 
            $("#"+key).val(val); 
        }); 
    }); 
}

4 个答案:

答案 0 :(得分:1)

这是一个简单的代码片段,如何访问你的json-keys:

$(document).ready(function(){

msg = '[{ "GameID":"1109100", "StartTime": "4:00 PM", "EndTime": "1/1/1900 5:30:00 PM", "Duration": "90","TeamID":"101567"},{ "GameID":"1108768", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"109873"},{ "GameID":"1108776", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"117674"},{ "GameID":"1108742", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24621"},{ "GameID":"1108751", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24622"},{ "GameID":"1108757", "StartTime": "10:30 AM", "EndTime": "1/1/1900 12:00:00 PM", "Duration": "90","TeamID":"24623"},{ "GameID":"1109098", "StartTime": "2:30 PM", "EndTime": "1/1/1900 4:00:00 PM", "Duration": "90","TeamID":"24746"}] ';


$.each(JSON.parse(msg), function(idx, obj) {
    var gameid = obj.GameID;
    var starttime = obj.StartTime;
    var endtime = obj.EndTime;
});

});

下次请在google上阅读更多关于使用jquery或javascript解析和访问json的内容;)

答案 1 :(得分:0)

尝试使用JSON.parse()Array#filterArray#map

var data = '[{ "GameID":"1109100", "StartTime": "4:00 PM", "EndTime": "1/1/1900 5:30:00 PM", "Duration": "90","TeamID":"101567"},{ "GameID":"1108768", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"109873"},{ "GameID":"1108776", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"117674"},{ "GameID":"1108742", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24621"},{ "GameID":"1108751", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24622"},{ "GameID":"1108757", "StartTime": "10:30 AM", "EndTime": "1/1/1900 12:00:00 PM", "Duration": "90","TeamID":"24623"},{ "GameID":"1109098", "StartTime": "2:30 PM", "EndTime": "1/1/1900 4:00:00 PM", "Duration": "90","TeamID":"24746"}]';

function finding(id){
var f = JSON.parse(data).filter(a=> a.TeamID == id) //it will the matched teamId
return f.map(a=> {return  {
   GameID : a.GameID,
   StartTime: a.StartTime,
   EndTime : a.EndTime,
}}) // it add compress with three key value and iterate the all matched object's
}
console.log(finding(109873))

答案 2 :(得分:0)



c

var yourJsonStr='[{ "GameID":"1109100", "StartTime": "4:00 PM", "EndTime": "1/1/1900 5:30:00 PM", "Duration": "90","TeamID":"101567"},{ "GameID":"1108768", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"109873"},{ "GameID":"1108776", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"117674"},{ "GameID":"1108742", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24621"},{ "GameID":"1108751", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24622"},{ "GameID":"1108757", "StartTime": "10:30 AM", "EndTime": "1/1/1900 12:00:00 PM", "Duration": "90","TeamID":"24623"},{ "GameID":"1109098", "StartTime": "2:30 PM", "EndTime": "1/1/1900 4:00:00 PM", "Duration": "90","TeamID":"24746"}]';

var gameInfo=eval("("+yourJsonStr+")");

$.each(gameInfo,function(i,item){
  $('#gameId').append((i + 1) + ": " + item.GameID + "<br/>");
});
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果它是一个字符串,则解析JSON,然后使用遍历对象数组 Array.find()

来自文档:

  

find()方法返回数组中第一个元素的值   满足提供的测试功能。否则未定义   返回。

   let json = JSON.parse(yourJsonString);
   let teamID= yourTeamID;
    //json is an array of javascript objects.  Iterate over them with "find";
    //This will return the first instance where the expression evaluates to true
    let foundObject = json.find(obj=>{
      return obj.TeamID=== teamID;
    });
    
    let gameID = foundObject.GameID;
    let startTime = foundObject.StartTime;
    let endTime = foundObject.EndTime;
    
    

希望这有帮助。