使用jquery在xml文件中解析json

问题描述 投票:0回答:1

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

<?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个回答
0
投票

使用jQuery可以很容易地遍历xml,就像使用html一样。

    $.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

© www.soinside.com 2019 - 2024. All rights reserved.