使用XML2JSON转换后解析JSON

时间:2019-05-02 18:23:00

标签: arrays node.js express

调用api并以xml格式获取响应。我正在尝试使用xml2json将xml输出转换为JSON。现在我该如何直接从json输出中读取温度参数值。

我在代码中使用了以下

var xml = response.body;
var result = convert.xml2json(xml, {compact: true, spaces: 4});
const jsonData = JSON.parse(result);
res.send(jsonData);

jsonData具有所有数据,但我只想读取例如温度值

这是转换后的json输出,

 {
        "_declaration": {
        "_attributes": {
        "version": "1.0",
        "encoding": "UTF-8"
        }
    },
    "wfs:FeatureCollection": {
        "_attributes": {
            "timeStamp": "2019-05-02T17:21:05Z",
            "numberReturned": "864",
            "numberMatched": "864",
            "xmlns:wfs": "http://www.opengis.net/wfs/2.0",
            "xmlns:gml": "http://www.opengis.net/gml/3.2",
            "xmlns:BsWfs": "http://xml.fmi.fi/schema/wfs/2.0",
            "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
            "xsi:schemaLocation": "http://www.opengis.net/wfs/2.0 
    http://schemas.opengis.net/wfs/2.0/wfs.xsd\n                        
    http://xml.fmi.fi/schema/wfs/2.0 
    http://xml.fmi.fi/schema/wfs/2.0/fmi_wfs_simplefeature.xsd"
        },
        "wfs:member": [
            {
                "BsWfs:BsWfsElement": {
                    "_attributes": {
                        "gml:id": "BsWfsElement.1.1.1"
                    },
                    "BsWfs:Location": {
                        "gml:Point": {
                            "_attributes": {
                                "gml:id": "BsWfsElementP.1.1.1",
                                "srsDimension": "2",
                                "srsName": "http://www.opengis.net/def/crs/EPSG/0/4258"
                            },
                            "gml:pos": {
                                "_text": "60.20520 24.65220 "
                            }
                        }
                    },
                    "BsWfs:Time": {
                        "_text": "2019-05-02T18:00:00Z"
                    },
                    "BsWfs:ParameterName": {
                        "_text": "GeopHeight"
                    },
                    "BsWfs:ParameterValue": {
                        "_text": "36.57"
                    }
                }
            },
            {
                "BsWfs:BsWfsElement": {
                    "_attributes": {
                        "gml:id": "BsWfsElement.1.1.2"
                    },
                    "BsWfs:Location": {
                        "gml:Point": {
                            "_attributes": {
                                "gml:id": "BsWfsElementP.1.1.2",
                                "srsDimension": "2",
                                "srsName": 
    "http://www.opengis.net/def/crs/EPSG/0/4258"
                            },
                            "gml:pos": {
                                "_text": "60.20520 24.65220 "
                            }
                        }
                    },
                    "BsWfs:Time": {
                        "_text": "2019-05-02T18:00:00Z"
                    },
                    "BsWfs:ParameterName": {
                        "_text": "Temperature"
                    },
                    "BsWfs:ParameterValue": {
                        "_text": "2.97"
                    }
                }
            }
      ]
         }
    }

1 个答案:

答案 0 :(得分:0)

jsonData['wfs:FeatureCollection']['wfs:member'][0]['BsWfs:BsWfsElement']['BsWfs:ParameterName']['_text']

ex)

jsonData = { "key0": 34 }
jsonArray = [ 35,36,37 ]

=>

console.log(jsonData['key0"]); // 34
console.log(jsonArray[0]); // 35