js中从json格式数据中获取特定对象

写个方法获取:

function getJsonValue(obj,name){
        var result = null;
        var value  = null;
        for(var key in obj){        
            value = obj[key];
            if(key == name){
            return value;
        } else {
            if( typeof value == "object" ){
            result = getJsonValue(value,name);
            }
        }
    }
    return result;
}
var jsonobj = { "semantic":{"taskId":"8.4.3"},"history":"cn.xxxx.fund"};
var taskId = getJsonValue(jsonobj,"history");
console.log(taskId);

猜你喜欢

转载自blog.csdn.net/qq_37706228/article/details/83152827