使用Angular 5 / Typescript从转换后的JSON中检索值

时间:2017-11-07 17:45:40

标签: json xml angular typescript

我刚从Angular5使用 SOAP HTTPClient 检索了一个对象,并且我在检索特定值时遇到了问题。 问题是,我只能使用以下方式访问所有这些:

         import {parseString} from 'xml2js';
         ...

         parseString(data, function (err, result) {
           var stringified = JSON.stringify(result);
           console.log(stringified);

          }

例如,下面的是我转换为JSON的XML 。我有两个问题:

1)我如何检索特定的<<值>>在里面? 例如 id 名称活动 ... InSpec OutSpec MiddleSpec 以及其他?

{
    "soap:Envelope": {
        "$": {
            "xmlns:soap": "http://www.w3.org/2003/05/soap-envelope",
            "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
            "xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
        },
        "soap:Body": [{
            "List": [{
                "$": {
                    "xmlns": "http://WebService/"
                },
                "ListResult": [{
                    "Total": ["1"],
                    "Dados": [{
                        "Posicao": [{
                            "id": ["<<value>>"],
                            "name": ["<<value>>"],
                            "event": ["<<value>>"],
                            "address": ["<<value>>"],
                            "description": ["<<value>>"],
                            "addressid": ["<<value>>"],
                            "number": ["<<value>>"],
                            "city": ["<<value>>"],
                            "descriptionEvent": ["<<value>>"],
                            "lat": ["<<value>>"],
                            "long": ["<<value>>"],
                            "In": [{
                                "InSpec": [{
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["true"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }]
                            }],
                            "Out": [{
                                "OutSpec": [{
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }]
                            }],
                            "Middle": [{
                                "MiddleSpec": [{
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["506"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["0"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["0"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["8"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["0"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["12.54"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["4658"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["4.1"]
                                }]
                            }]
                        }]
                    }]
                }]
            }]
        }]
    }
}

2)从XML转换为JSON以检索这些值是否更好?是否可以直接从XML获取它们?

感谢帮助。

1 个答案:

答案 0 :(得分:1)

假设你有一个字符串化的json。这工作

let jsonObject = JSON.parse(stringified);
console.log('jsonObject', jsonObject);
let jsonEnvelope = jsonObject['soap:Envelope'];
let jsonBody = jsonEnvelope['soap:Body'];
console.log(jsonBody);
let jsonList = jsonBody[0];
console.log(jsonList);
let jsonListResult = jsonList['List'];
console.log(jsonListResult);

当然,正确的答案是为您的肥皂反应创建一个映射器和正确的模型。