简体   繁体   English

将XML数据解析为JSON

[英]Parse XML data to JSON

In our environment we use MS SQL w/ stored procedures. 在我们的环境中,我们使用带有存储过程的MS SQL。 In those procedures we return our results as XML and when access the data as we need to. 在这些过程中,我们以XML形式返回结果,并在需要时访问数据。

I'm introducing some charts into our tools which are 3rd party and require specific formats in order to operate and I am running into an issue. 我正在向我们的第三方工具介绍一些图表,这些图表需要特定的格式才能进行操作,但我遇到了问题。

In this screenshot, you are able to see what the structure should look like which I can get to work with the plugin just fine. 在此屏幕截图中,您可以看到结构看起来应该是什么样的,我可以很好地使用该插件。 The issue is with how SimpleXML handles single result sets. 问题在于SimpleXML如何处理单个结果集。

在此处输入图片说明

As you can see in the image below, with one result item, it is no longer formatted as an array. 如下图所示,带有一个结果项,它不再格式化为数组。 The problem being is that the plugin expects to find the data in the format in the first example, but when there is only one value, it doesn't store it as an array. 问题在于,该插件希望在第一个示例中以该格式查找数据,但是当只有一个值时,它不会将其存储为数组。

在此处输入图片说明

As you can see from this image, the dataset for the escalationTypes is in the array format where the one below, submittedByDepartment is not. 从该图像可以看到, escalationTypes的数据集采用的数组格式不是下面的submittedByDepartment 在此处输入图片说明

I am trying to find out if there is something I can do to fix the root of this problem, with SimpleXML. 我正在尝试使用SimpleXML找出是否可以解决此问题的根源。 Is this a common issue found with SimpleXML with a workaround? 这是带有解决方法的SimpleXML的常见问题吗?

UPDATE UPDATE

Here is a sample of the XML Object I am working with: http://pastebin.com/uPh0m3qX 这是我正在使用的XML对象的示例: http : //pastebin.com/uPh0m3qX

I'm not 100% clear what your required structure is, but in general, it's not a good idea to jump straight from XML to JSON or vice versa. 我不是100%清楚您所需的结构是什么,但是总的来说,直接从XML转换为JSON或反之亦然不是一个好主意。 Both can represent data in various ways, and what you really want is to extract data from one and turn it into the other. 两者都可以以多种方式表示数据,而您真正想要的是从一种数据中提取数据并将其转换为另一种数据。

Note that this is what SimpleXML is designed to help you with - it never contains any arrays, but it gives you an API which helps you extract the data you need. 请注意,这就是SimpleXML旨在帮助您的-它从不包含任何数组,但是为您提供了一个API ,可帮助您提取所需的数据。

If I understand correctly, you want to build an array from each of the dataset elements, and put those into your JSON, so you'd want something like this: 如果我理解正确,则希望从每个dataset元素构建一个数组,并将其放入JSON中,因此需要以下内容:

foreach ( $xml->children() as $item_name => $item ) {
      foreach ( $item->dataset as $dataset ) {
             $json[$item_name]['dataset'] = (array)$dataset->attributes();
       }
}

Note that neither of those loops will behave differently if there is only one item to loop over. 请注意,如果只有一个项目要循环,则这些循环都不会有不同的行为。 SimpleXML decides whether to behave like an array or an object based on how you use it, not based on what the XML looks like. SimpleXML根据使用方式而不是基于XML的外观来决定其行为类似于数组还是对象。

Note that while you can build a more general XML to array (or XML to JSON) function, getting it to always give the desired output from any input will probably take more time, and lead to harder-to-debug code, than writing specific code like the above. 请注意,虽然您可以构建更通用的XML到数组(或XML到JSON)功能,但要使其始终提供任何输入所需的输出,则可能会花费更多时间,并且比编写特定的代码会导致难以调试的代码像上面的代码。

you can declare the object as an array by adding (array) before referencing the variable. 您可以通过在引用变量之前添加(数组)来将对象声明为数组。 (array)$wasobject

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

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