如何解析来自 JSON 的@name of license
How to parse @name of license from JSON
我有以下 JSON 需要拉取 @name
"@name": "Reuters TV iOS",
"license": [{
"@name": "Apache License 2.0",
"component": [{
"name": "RMStore",
"url": "http://github.com/robotmedia/RMStore/"
}, {
"name": "mixpanel-iphone",
"url": "http://github.com/mixpanel/mixpanel-iphone/"
}, {
"name": "facebook-ios-sdk",
"url": "http://github.com/facebook/facebook-ios-sdk/"
}],
"licensetext": "text "
}, {
"@name": "Apple MIT License",
"component": {
"name": "Apple iOS Developer Library",
"url": ""
},
"licensetext": " text"
}, {
"@name": "BSD 3-clause \"New\" or \"Revised\" License",
"component": [{
"name": "Reachability-DDG",
"url": ""
}, {
"name": "Tweaks",
"url": "http://github.com/facebook/Tweaks/"
}, {
"name": "NYXImagesKit",
"url": "http://github.com/Nyx0uf/NYXImagesKit/"
}, {
"name": "pop",
"url": "http://github.com/facebook/pop/"
}, {
"name": "JLRoutes",
"url": "http://github.com/joeldev/JLRoutes/"
}],
"licensetext": "text "
}, {
"@name": "Deusty BSD- License",
"component": {
"name": "CocoaLumberjack",
"url": "http://github.com/CocoaLumberjack/CocoaLumberjack/"
},
"licensetext": " text"
}, {
"@name": "MIT License",
"component": [{
"name": "SDWebImage",
"url": "http://github.com/rs/SDWebImage/"
}, {
"name": "SVProgressHUD",
"url": "http://github.com/TransitApp/SVProgressHUD/"
}, {
"name": "TableViewCellWithAutoLayout",
"url": ""
}, {
"name": "OHHTTPStubs",
"url": "http://github.com/AliSoftware/OHHTTPStubs/"
}, {
"name": "XCTestAsync",
"url": "http://github.com/touchableideas/XCTestAsync/"
}, {
"name": "objection",
"url": "http://github.com/atomicobject/objection/"
}, {
"name": "houston",
"url": "http://github.com/nomad/houston/"
}, {
"name": "YLMoment",
"url": "http://github.com/YannickL/YLMoment/"
}, {
"name": "ReactiveCocoa",
"url": "http://github.com/ReactiveCocoa/ReactiveCocoa/"
}, {
"name": "cocos2d-iphone",
"url": "http://github.com/cocos2d/cocos2d-iphone/"
}, {
"name": "Aspects",
"url": "http://github.com/steipete/Aspects/"
}, {
"name": "TTTAttributedLabel",
"url": "http://github.com/mattt/TTTAttributedLabel/"
}, {
"name": "Mantle",
"url": "http://github.com/Mantle/Mantle/"
}, {
"name": "Masonry",
"url": "http://github.com/Masonry/Masonry/"
}, {
"name": "apple_push_certs",
"url": "http://rubygems.org/gems/apple_push_certs"
}, {
"name": "HockeySDK-iOS",
"url": "http://github.com/bitstadium/HockeySDK-iOS/"
}],
"licensetext": " text "
}, {
"@name": "OpenSSL Combined License",
"component": {
"name": "OpenSSL",
"url": "http://www.openssl.org/"
},
"licensetext": "text"
}, {
"@name": "SIL Open Font License 1.1",
"component": {
"name": "Font-Awesome",
"url": "http://github.com/FortAwesome/Font-Awesome/"
},
"licensetext": "text"
}, {
"@name": "zlib License",
"component": [{
"name": "iCarousel",
"url": "http://github.com/nicklockwood/iCarousel/"
}, {
"name": "FXKeychain",
"url": "http://github.com/nicklockwood/FXKeychain/"
}, {
"name": "FXForms",
"url": "http://github.com/nicklockwood/FXForms/"
}],
"licensetext": "text"
}],
"isProjectComplete": "true"
};
我可以得到 name
、URL
和 license
,但不知道如何让 @name
显示在顶部,例如 "Apache License 2.0".
这是当前状态:jsfiddle
使用bracket notation,像这样
console.log(lic['@name']);
如果我没理解错的话,你的问题是你不能在 JS 属性 名称中使用像 @ 这样的特殊字符。
只需使用括号:json['@name']
我有以下 JSON 需要拉取 @name
"@name": "Reuters TV iOS",
"license": [{
"@name": "Apache License 2.0",
"component": [{
"name": "RMStore",
"url": "http://github.com/robotmedia/RMStore/"
}, {
"name": "mixpanel-iphone",
"url": "http://github.com/mixpanel/mixpanel-iphone/"
}, {
"name": "facebook-ios-sdk",
"url": "http://github.com/facebook/facebook-ios-sdk/"
}],
"licensetext": "text "
}, {
"@name": "Apple MIT License",
"component": {
"name": "Apple iOS Developer Library",
"url": ""
},
"licensetext": " text"
}, {
"@name": "BSD 3-clause \"New\" or \"Revised\" License",
"component": [{
"name": "Reachability-DDG",
"url": ""
}, {
"name": "Tweaks",
"url": "http://github.com/facebook/Tweaks/"
}, {
"name": "NYXImagesKit",
"url": "http://github.com/Nyx0uf/NYXImagesKit/"
}, {
"name": "pop",
"url": "http://github.com/facebook/pop/"
}, {
"name": "JLRoutes",
"url": "http://github.com/joeldev/JLRoutes/"
}],
"licensetext": "text "
}, {
"@name": "Deusty BSD- License",
"component": {
"name": "CocoaLumberjack",
"url": "http://github.com/CocoaLumberjack/CocoaLumberjack/"
},
"licensetext": " text"
}, {
"@name": "MIT License",
"component": [{
"name": "SDWebImage",
"url": "http://github.com/rs/SDWebImage/"
}, {
"name": "SVProgressHUD",
"url": "http://github.com/TransitApp/SVProgressHUD/"
}, {
"name": "TableViewCellWithAutoLayout",
"url": ""
}, {
"name": "OHHTTPStubs",
"url": "http://github.com/AliSoftware/OHHTTPStubs/"
}, {
"name": "XCTestAsync",
"url": "http://github.com/touchableideas/XCTestAsync/"
}, {
"name": "objection",
"url": "http://github.com/atomicobject/objection/"
}, {
"name": "houston",
"url": "http://github.com/nomad/houston/"
}, {
"name": "YLMoment",
"url": "http://github.com/YannickL/YLMoment/"
}, {
"name": "ReactiveCocoa",
"url": "http://github.com/ReactiveCocoa/ReactiveCocoa/"
}, {
"name": "cocos2d-iphone",
"url": "http://github.com/cocos2d/cocos2d-iphone/"
}, {
"name": "Aspects",
"url": "http://github.com/steipete/Aspects/"
}, {
"name": "TTTAttributedLabel",
"url": "http://github.com/mattt/TTTAttributedLabel/"
}, {
"name": "Mantle",
"url": "http://github.com/Mantle/Mantle/"
}, {
"name": "Masonry",
"url": "http://github.com/Masonry/Masonry/"
}, {
"name": "apple_push_certs",
"url": "http://rubygems.org/gems/apple_push_certs"
}, {
"name": "HockeySDK-iOS",
"url": "http://github.com/bitstadium/HockeySDK-iOS/"
}],
"licensetext": " text "
}, {
"@name": "OpenSSL Combined License",
"component": {
"name": "OpenSSL",
"url": "http://www.openssl.org/"
},
"licensetext": "text"
}, {
"@name": "SIL Open Font License 1.1",
"component": {
"name": "Font-Awesome",
"url": "http://github.com/FortAwesome/Font-Awesome/"
},
"licensetext": "text"
}, {
"@name": "zlib License",
"component": [{
"name": "iCarousel",
"url": "http://github.com/nicklockwood/iCarousel/"
}, {
"name": "FXKeychain",
"url": "http://github.com/nicklockwood/FXKeychain/"
}, {
"name": "FXForms",
"url": "http://github.com/nicklockwood/FXForms/"
}],
"licensetext": "text"
}],
"isProjectComplete": "true"
};
我可以得到 name
、URL
和 license
,但不知道如何让 @name
显示在顶部,例如 "Apache License 2.0".
这是当前状态:jsfiddle
使用bracket notation,像这样
console.log(lic['@name']);
如果我没理解错的话,你的问题是你不能在 JS 属性 名称中使用像 @ 这样的特殊字符。
只需使用括号:json['@name']