【问题标题】:parse json in xml file with jquery使用 jquery 解析 xml 文件中的 json
【发布时间】:2018-09-01 12:25:35
【问题描述】:

这是我的文件,我如何解析它以找到“描述”、“注册日期”等元素? 谢谢你的帮助。

<?xml version="1.0" encoding="utf-8"?>
<Vehicle xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://regcheck.org.uk">
<vehicleJson>{ "Description": "TOYOTA YARIS II", "RegistrationYear": "2006", "CarMake": { "CurrentTextValue": "TOYOTA" }, "CarModel": { "CurrentTextValue": "YARIS II" }, "EngineSize": { "CurrentTextValue": "5" }, "FuelType": { "CurrentTextValue": "DIESEL" }, "MakeDescription": { "CurrentTextValue": "TOYOTA" }, "ModelDescription": { "CurrentTextValue": "YARIS II" }, "Immobiliser": { "CurrentTextValue": "" }, "IndicativeValue": { "CurrentTextValue": 0 }, "DriverSide": { "CurrentTextValue": "" }, "BodyStyle": { "CurrentTextValue": "BERLINE 3 PORTES" }, "RegistrationDate": "2006-10-26", "ImageUrl": "http://immatriculationapi.com/image.aspx/@VE9ZT1RBIFlBUklTIElJ", "ExtendedData": { "anneeSortie": "2006", "boiteDeVitesse": "", "carburantVersion": "D", "carrosserieVersion": "", "classeSra": "G", "libVersion": "1.4 D-4D", "libelleModele": "YARIS II", "marque": "TO", "modele": "32", "produit": "", "puissance": "5", "version": "", "cleCarrosserie": "", "groupeSra": "30", "nbPlace": "", "datePremiereMiseCirculation": "01012006", "questionBatterie": "", "electrique": "", "genre": "", "typeVehicule": "", "numSerieMoteur": "VNKJC98390A048869", "valeurANeufSRA": "", "niveauRisqueVol": "", "protectionConstructeur": "", "puissanceDyn": "", "segmentVeh": "" } }
</vehicleJson>
    <vehicleData>
    <Description>TOYOTA YARIS II</Description>
    <RegistrationYear>2006</RegistrationYear>
    <CarMake>
        <CurrentTextValue>TOYOTA</CurrentTextValue>
    </CarMake>
    <CarModel>YARIS II</CarModel>
    <BodyStyle>
        <CurrentTextValue>BERLINE 3 PORTES</CurrentTextValue>
    </BodyStyle>
    <EngineSize>
        <CurrentTextValue>5</CurrentTextValue>
    </EngineSize>
    <FuelType>
        <CurrentTextValue>DIESEL</CurrentTextValue>
    </FuelType>
    <Immobiliser>
        <CurrentTextValue />
    </Immobiliser>
    </vehicleData>
</Vehicle>

【问题讨论】:

标签: jquery json xml


【解决方案1】:

使用 jQuery 非常简单,它可以像遍历 html 一样遍历 xml。

    $.get('data.xml',function(xml){

     var jsonString = $(xml).find('vehicleJson').text();
     console.log(jsonString);

     var parsedObj = JSON.parse(jsonString)
     console.log('Description =' ,parsedObj.Description);

  });

Working demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多