解析JSON数据并获取一个Object

时间:2015-05-15 10:10:40

标签: javascript jquery json sapui5

我在SAPUI5应用程序中获得了一个简单的JSON数据结构,其中包含电话号码,传真号码和手机号码:

{
    "teles": [
                {"tele": "05312024040", "default": true},
                {"tele": "017666254336", "default": false},
                {"tele": "017666224336", "default": false}
    ],
    "faxs": [
                {"fax":"053155599755", "default": true},
                {"fax":"01548568745", "default": false}
    ],
    "mobils": [
                {"mobil":"017655994816", "default": true},
                {"mobil":"01548568745", "default": false}
    ]
 }

我的目标是循环/解析数据结构并获取默认数字,并将其放入名为" tele-standard"," fax-standard&#的另一个模型中34;或者" mobil-standard"。这应该在显示数据之前发生,因此可以将它放入可编辑的TextField中:

Four TextFields with an option to open a dialog to edit.

我根本不知道如何使用jquery,或sapui5或javascript这样做,所以我可以实现我的目标。你能帮忙吗?

新模型中的结果应如下所示:

{
    "archived": "-", 
    "nummer": "12", 
    "anrede": "Herr",
    "vorname": "Osamah", 
    "nachname": "Al-Doaiss",
    "tele-default": "05312024040",
    "fax-defaule": "053155599755",
    "mobil-default": "017655994816"
}

TL; DR:我有一个带阵列的JSON。我想解析它们并获得默认号码。不知道怎么做!

4 个答案:

答案 0 :(得分:2)

看到这个



var numbers = {
    "teles": [
                {"tele": "05312024040", "default": true},
                {"tele": "017666254336", "default": false},
                {"tele": "017666224336", "default": false}
    ],
    "faxs": [
                {"fax":"053155599755", "default": true},
                {"fax":"01548568745", "default": false}
    ],
    "mobils": [
                {"mobil":"017655994816", "default": true},
                {"mobil":"01548568745", "default": false}
    ]
 };

function getAllDefaultNumbers(nums) {
    var outObj = {};
    (Object.keys(nums) || []).forEach(function (number_type) {
        (nums[number_type] || []).forEach(function (obj) {
            if (obj.default) {
                outObj[number_type + '-default'] = obj.tele || obj.fax || obj.mobil;
            }
        });
    });
    return outObj;
}

document.getElementById('out').innerHTML = JSON.stringify(getAllDefaultNumbers(numbers));

<div id="out"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

试试这个

1

var data = {
    "teles": [
        {"tele": "05312024040", "default": true},
        {"tele": "017666254336", "default": false},
        {"tele": "017666224336", "default": false}
    ],
    "faxs": [
        {"fax":"053155599755", "default": true},
        {"fax":"01548568745", "default": false}
    ],
    "mobils": [
        {"mobil":"017655994816", "default": true},
        {"mobil":"01548568745", "default": false}
    ]
};

var model = {
    "archived": "-", 
    "nummer": "12", 
    "anrede": "Herr",
    "vorname": "Osamah", 
    "nachname": "Al-Doaiss"
};

Object.keys(data).forEach(function (key) {
    data[key].forEach(function (el) {
        var keys = Object.keys(el),
            key  = keys.join('-');

        if (el.default && el.default === true && !model[key]) {
            model[keys.join('-')] = el[keys[0]];
        }
    });
});

console.log(model);

2

var data = {
    "teles": [
        {"tele": "05312024040", "default": true},
        {"tele": "017666254336", "default": false},
        {"tele": "017666224336", "default": false}
    ],
    "faxs": [
        {"fax":"053155599755", "default": true},
        {"fax":"01548568745", "default": false}
    ],
    "mobils": [
        {"mobil":"017655994816", "default": true},
        {"mobil":"01548568745", "default": false}
    ]
};

var model = {
    "archived": "-", 
    "nummer": "12", 
    "anrede": "Herr",
    "vorname": "Osamah", 
    "nachname": "Al-Doaiss"
};

Object.keys(data).forEach(function (key) {
    data[key].forEach(function (el) {
        var keys = Object.keys(el),
            key  = keys.join('-');

        if (el.default && el.default === true && !model[key]) {
            model[key] = el[keys[0]];
        }
    });
});

console.log(model);

答案 2 :(得分:1)

将数据视为您的对象:

for(var phone in data.teles) {
    if(phone.default == true) {
        // Example on how to set phone number with jquery
        $('#defaultPhone').val(phone.tele);
        // You would need an input with the id defaultPhone for this
    }
}

编辑:关于您的更新,您可能会喜欢这样(将变量结果视为结果对象)

result.tele-default = phone.tele //instead of the jquery part in the upper example

答案 3 :(得分:1)

以下代码段用于获取默认电话号码。 val将存储您的JSON数据。

     objJson =   JSON.parse(vals);
      var tele_default;
      if(objJson.teles.count>0){
        for(int i=0;i<objJson.teles.count;i++){
            if(objJson.teles[i].default == "true"){
                tele_default = objJson.teles[i].tele
             }
          }
      }