简体   繁体   中英

xml to json conversion and vice versa angular 4

I am working in a angular 4 project and i have to convert xml to json and vice versa. I am successfull in achieving this by using below thread: https://goessner.net/download/prj/jsonxml/

This is my sample xml :

 <food><pizza type="cheese"/></food>

The parser converted it to json as below:

{
"food":{
 "pizza":{"@type":"cheese"},

I have to write a selector where i need to render value of @type. I have tried it like below:

//selector which returns @type

   export const foodValueSelctr= 
   createSelector(paredJsonSelectore,
       (json) => {
          console.log(json.food.pizza.@type);
          return json.food.pizza.@type;
    });

At this line of code I get below error:

Module parse failed: Unexpected token (18:38)
You may need an appropriate loader to handle this file type.

How can I access this @type in code using dot notation ? Also, Can i use any other lib to achieve my functionality ?

You can use the alternative syntax (known as "bracket notation") as follows:

console.log(json.food.pizza["@type"]);

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors for documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM