본문 바로가기

Web/Javascript

string to json

javascript 에서 string 을 json 으로 변경 시키는 방법은 다음과 같다.
[code]
var otherString = '{"aspect": "' + key + '", "property": "' + statusList[key][key2] + '"}';
var jsonData = eval('(' + otherString + ')');
[/code]
예)
[code]var statusList = {
"Battery" : ["batteryLevel", "batteryBeingCharged"],
"Device" : ["imei", "model", "version", "vendor"],
"Display" : ["resolutionHeight", "pixelAspectRatio", "dpiY", "resolutionWidth", "dpiX"],
"MemoryUnit" : ["size", "removable", "availableSize"],
"OperatingSystem" : ["language", "version", "name", "vendor"],
"CellularHardware" : ["status"],
"CellularNetwork" : ["isInRoaming", "mcc", "mnc", "signalStrength", "operatorName"],
"WiFiHardware" : ["status"],
"WiFiNetwork" : ["ssid", "signalStrength", "networkStatus"],
"WebRuntime" : ["wacVersion", "supportedImageFormats", "version", "name", "vendor"]
};

var propertyValueCount = 0;
function checkProperties() {
    for (var key in statusList) {
        for (var key2 in statusList[key]) {
            var prop = '{"aspect": "' + key + '", "property": "' + statusList[key][key2] + '"}';
            var jsonData = eval('('+prop+')'); // here
        }
    }
}
[/code]

'Web > Javascript' 카테고리의 다른 글

js minifier  (0) 2017.02.27
inheritance  (0) 2013.09.26
queue  (0) 2013.09.26
remove array  (0) 2013.09.26
callback  (0) 2013.09.26