简体   繁体   English

如何在Ionic2项目中使用xml2json

[英]How to use xml2json in Ionic2 project

I am developing a mobile app using Ionic2 framework. 我正在使用Ionic2框架开发移动应用程序。 I need to convert a xml file to json format. 我需要将xml文件转换为json格式。

For this I have installed "xml2json" using command "npm install xml2json --save". 为此,我已经使用命令“ npm install xml2json --save”安装了“ xml2json”。

This has saved a module named "xml2json" in "node_modules" folder. 这已将名为“ xml2json”的模块保存在“ node_modules”文件夹中。

I can also see it under dependencies in "package.json" file as "xml2json": "^0.9.1" 我还可以在“ package.json”文件中的依赖项下以“ xml2json”的形式查看它:“ ^ 0.9.1”

Now when I declared it in the .ts file as "import * as xml2json from "xml2json";", I am receiving the following error: "can not find module xml2json" 现在,当我在.ts文件中将其声明为“从* xml2json导入*为xml2json;”时,我收到以下错误:“找不到模块xml2json”

I guess I need to install the typings for this module. 我想我需要为此模块安装类型。 I have tried all the possible typings commands but could not install it. 我已经尝试了所有可能的键入命令,但无法安装。 I received errors like 我收到了类似的错误

typings ERR! 键入ERR! caused by https://api.typings.org/entries/dt/xml2json/versions/latest responded with 404, expected it to equal 200 https://api.typings.org/entries/dt/xml2json/versions/latest引起的响应为404,预期等于200

Please let me know how can I install typings for xml2json OR how can I use xml2json in my project. 请让我知道如何安装xml2json的类型或如何在项目中使用xml2json。 Thanks. 谢谢。

My system information: 我的系统信息:

typings version - 1.3.2 打字版本-1.3.2

Cordova CLI: 6.3.0 Gulp version: CLI version 3.9.1 Gulp local: Local version 3.9.1 Ionic Framework Version: 2.0.0-beta.11 Ionic CLI Version: 2.0.0-beta.36 Ionic App Lib Version: 2.0.0-beta.19 OS: Distributor ID: Ubuntu Description: Ubuntu 16.04 LTS Node Version: v6.3.0 Cordova CLI:6.3.0 Gulp版本:CLI版本3.9.1 Gulp本地版本:本地版本3.9.1 Ionic Framework版本:2.0.0-beta.11 Ionic CLI版本:2.0.0-beta.36 Ionic App Lib版本:2.0 .0-beta.19操作系统:发行商ID:Ubuntu说明:Ubuntu 16.04 LTS节点版本:v6.3.0

You can. 您可以。 Just add your node_modules/path_to_xml2json.js to your index.html and declare it in your component : declare var xml2json: any; 只需将您的node_modules/path_to_xml2json.js添加到index.html并将其在您的组件中declare var xml2json: any;

It's not a typescript module, so you can import it like you did. 它不是打字稿模块,因此您可以像导入一样导入它。

You can install the typings for xml2json now using the command 您现在可以使用以下命令安装xml2json的类型

typings install xml2json --save 键入install xml2json --save

For Parsing xml to json in ionic 2 You can install the typings for xml2json now using the command 对于在ionic 2中将xml解析为json,您现在可以使用以下命令安装xml2json的类型

typings install xml2json --save

If it generate error like this 如果它产生这样的错误

'typings' is not recognized as an internal or external command,
operable program or batch file.

Then first use this command to install typing 然后先使用此命令安装打字

npm install typings -g

Then run previous command 然后运行上一个命令

typings install xml2json --save

Then it'll work fine. 这样就可以了。 Hope it work for you 希望它对你有用

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

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