【问题标题】:Parsing JSON after conversion using XML2JSON使用 XML2JSON 转换后解析 JSON
【发布时间】:2019-05-03 16:32:57
【问题描述】:

调用 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"
                    }
                }
            }
      ]
         }
    }

【问题讨论】:

    标签: arrays node.js express


    【解决方案1】:
    jsonData['wfs:FeatureCollection']['wfs:member'][0]['BsWfs:BsWfsElement']['BsWfs:ParameterName']['_text']
    

    前)

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

    =>

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

    【讨论】:

    • 感谢您的回答。你能用几句话来解释这是如何工作的吗?
    • @King Stone 感谢您的回答。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多