从json数据parsing值
我试图使用node.js
从json
数据中获取值
以下是json数据(它是dynamic的),它可能包含像tcp,ipp,http,udp,https
名称。这里我只显示tcp
和ipp
sample json [ { '$': { name: 'tcp', showname: 'tnternet crinting Protocol', size: '584', pos: '202' }, field: [ [Object], [Object], [Object], [Object] ] }, { '$': { name: 'ipp', showname: 'Internet Printing Protocol', size: '584', pos: '202' }, field: [ [Object], [Object], [Object], [Object], [Object], [Object] ] } ]
我只需要获取ipp
的详细信息。
(没有使用sample[1]
任何其他方法,比如来自json数据的name =ipp
)
example { '$': { name: 'ipp', showname: 'Internet Printing Protocol', size: '584', pos: '202' }, field: [ { '$': [Object] }, { '$': [Object] }, { '$': [Object] }, { '$': [Object], field: [Object] }, { '$': [Object], field: [Object] }, { '$': [Object] } ] }
你可以使用lodash,如下所示:
var result = _.result(_.find(list, { '$': {'name':'ipp'} }), '$');
Plunker: http ://plnkr.co/edit/unh9wZuDCnIETE7EWHmD?p=preview
Lodash: https ://lodash.com/docs#find
你必须迭代,并寻找名称
var ipp = null; sample.forEach(function(o) { if ( o['$'].name === 'ipp' ) { ipp = o; return false; } }); // ipp === object here
小提琴