简体   繁体   English

xml到json的转换,反之亦然,角度4

[英]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. 我正在一个angular 4项目中工作,我必须将xml转换为json,反之亦然。 I am successfull in achieving this by using below thread: https://goessner.net/download/prj/jsonxml/ 我通过使用以下线程成功实现了这一目标: https : //goessner.net/download/prj/jsonxml/

This is my sample xml : 这是我的示例xml:

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

The parser converted it to json as below: 解析器将其转换为json,如下所示:

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

I have to write a selector where i need to render value of @type. 我必须编写一个选择器,在其中我需要呈现@type的值。 I have tried it like below: 我已经尝试过如下所示:

//selector which returns @type //返回@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 ? 如何使用点符号在代码中访问此@type? Also, Can i use any other lib to achieve my functionality ? 另外,我可以使用任何其他lib来实现我的功能吗?

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. 有关文档,请参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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