将XML数据转换为Json格式AngularJS [英] Convert XML Data to Json Format AngularJS

查看:264
本文介绍了将XML数据转换为Json格式AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从AngularJS使用Treeview指令。存储过程返回xml。树视图指令采用json格式。

I am trying to use Treeview directive from AngularJS. The stored procedure is returning xml.The tree view directive takes json format. The Controller will get the data from service.I am stuck trying to convert xml to json in service.

以下是xml结构:

<Company Data="New Company">
  <Manager Data="Working">
    <Employee Data="ABC" />
    <Employee Data="DEF" />
    <Employee Data="GHI">
      <SubEmployee Data="Approval">
        <Stuff Data="Financial" />
        <Stuff Data="Consol" />
      </SubEmployee>
      <SubEmployee Data="Rolled-Over">
        <Stuff Data="Corporate" />
      </SubEmployee>
    </Employee>
  </Manager>
</Company>

以下是预期的JSON:

Below is the expected JSON :

[
  {
    label: "New Company",
    id: "Company",
    children: [
      {
        label: "Working",
        id: "Manager",
        children: [
          {
            label: "ABC",
            id: "Employee",
            children: [

            ]
          },
          {
            label: "DEF",
            id: "Employee",
            children: [

            ]
          },
          {
            label: "GHI",
            id: "Employee",
            children: [
              {
                label: "Approval",
                id: "SubEmployee",
                children: [
                  {
                    label: "Financial",
                    id: "Stuff",
                    children: [

                    ]
                  },
                  {
                    label: "Consol",
                    id: "Stuff",
                    children: [

                    ]
                  }
                ]
              },
              {
                label: "RolledOver",
                id: "SubEmployee",
                children: [
                  {
                    label: "Corporate",
                    id: "Stuff",
                    children: [

                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]


推荐答案


  1. 以您需要的JSON格式返回API中的数据

  2. 将XML转换为JSON在你的角度应用程序使用javascript。

如果可以的话,我会推荐选项1。对于选项2,然后回答
使用Javascript将XML转换为JSON(和返回) 适用于Javascript中的XML / JSON转换。

I would recommend option 1 if that is possible. For option 2, then answer to "Convert XML to JSON (and back) using Javascript" is good for XML/JSON conversion in Javascript.

如果您阅读上述链接上的答案,您将看到为什么选择1更好。在这些格式之间转换可能有问题。

If you read the answers on the above link you will see why option 1 is preferable. Converting between these formats can be problematic.

这篇关于将XML数据转换为Json格式AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆