2014-01-23 77 views
0

我有一個json,我需要從json中獲取鍵值,其中值爲true。如何從json中獲取鍵值,其中的值爲true

這裏是我所期望的。

預期:

{ 
     "isChrome": true, 
     "isDesktop": true, 
     "isWindows": true 
} 

問題:

{ 
     "isMobile": false, 
     "isiPad": false, 
     "isiPod": false, 
     "isiPhone": false, 
     "isAndroid": false, 
     "isBlackberry": false, 
     "isOpera": false, 
     "isIE": false, 
     "isIECompatibilityMode": false, 
     "isSafari": false, 
     "isFirefox": false, 
     "isWebkit": false, 
     "isChrome": true, 
     "isKonqueror": false, 
     "isOmniWeb": false, 
     "isSeaMonkey": false, 
     "isFlock": false, 
     "isAmaya": false, 
     "isEpiphany": false, 
     "isDesktop": true, 
     "isWindows": true, 
     "isLinux": false, 
     "isMac": false, 
     "isBada": false, 
     "isSamsung": false, 
     "isRaspberry": false, 
     "isBot": false, 
     "isCurl": false, 
     "isAndroidTablet": false, 
     "Browser": "Chrome", 
     "Version": "32.0.1700.76", 
     "OS": "Windows 7", 
     "Platform": "Microsoft Windows", 
     "GeoIP": {}, 
     "source": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36" 
    } 

回答

1
var result = {}; 
for (var key in data) { 
    if (data[key] === true) { 
     result[key] = true; 
    } 
} 
console.log(result); 

輸出

{ isChrome: true, isDesktop: true, isWindows: true } 
0
var result = {}; 
for (var key in input.Problem) { 
    if (input.Problem[key] === true) { 
     result[key] = true; 
    } 
}