简体   繁体   English

用XML表示RESTful服务的List的最佳方法是什么,这样反序列化将更容易

[英]What is the best way to represent List in XML for a RESTful Service so that it will be easier to deserialize

I use two programming languages: Java and C#, and the two communicating through a RESTful service. 我使用两种编程语言:Java和C#,并且两者通过RESTful服务进行通信。 From Java, I sent some XML for a request made by the C# program, and the response for the GET include some list structure. 从Java,我为C#程序发出的请求发送了一些XML,并且GET的响应包括一些列表结构。

<X>
    <A>[3, 18, 11, 8, 19, 6]</A>
</X>
<X>
    <A>[3, 18, 11, 8, 19, 6]</A>
</X>

I have a difficult time parsing the string back into the list, not really difficult, but so manual. 我很难将字符串解析回到列表中,这并不是很困难,但是很手工。 What I do is: 我要做的是:

    public RoutingResults(String A) 
    {
        A = A.Replace("[", "");
        A = A.Replace("]", "");
        A = A.Replace(" ", "");
        String[] As = A.Split(new String[] {","}, StringSplitOptions.RemoveEmptyEntries);

        this.A = new List<string>();
        foreach (String ax in As)
        {
            this.As.Add(ax);
        }
    }

I think the code above is so funny, don't you think so? 我认为上面的代码太有趣了,您不这样认为吗? I feel so embarrassed if someone would someday hack into my PC and know that I had to code something that way. 如果有人总有一天会入侵我的PC并知道我必须以这种方式编写代码,我会感到很尴尬。 Haha. 哈哈。

So my question is, especially to you who are experienced with RESTful, and REST world, and XML, and anything is in that part of technology; 因此,我的问题是,特别是对于那些对RESTful,REST世界和XML有丰富经验的人,而这一切都属于技术领域。 what would be the ideal way to represent list in the XML for REST, and it would later be ideal as well to deserialize. 这是在REST XML中表示列表的理想方式,而后来反序列化也是理想的。 Please give certain code to exemplify, whether in C# xor Java. 请使用C#xor或Java语言给出一些示例代码。

If I had to sent the response like this: 如果必须发送这样的响应:

 <X>
      <A>
          <as>3</as>
          <as>18</as>
          <as>11</as>
          <as>8</as>
          <as>19</as>
          <as>6</as>
      <A>
 </X>

I think sending data like the above format would impose a performance penalty; 我认为发送类似上述格式的数据会导致性能下降; compared if data is formatted neatly, just a .toString representation of a typical java.util.List. 比较数据的格式是否整洁,只是典型的java.util.List的.toString表示形式。 Don't you think so? 你不是这样吗

I wouldn't have said there is much wrong with the original code to be honest. 老实说,我不会说原始代码有很多错误。 It does the job, it's only a few lines and it's clear how it does it. 它可以完成工作,只有几行,而且很清楚它是如何完成的。 You could change the last bit to: 您可以将最后一位更改为:

this.A = new List<string>(As);

...as the constructor of List<T> is able to take an array as an input and populate itself automatically. ...因为List<T>的构造函数能够将数组作为输入并自动填充自身。

You could also look into sending it as a JSON array, which would be similar and would not involve a lot of extra XML tags. 您也可以考虑将其作为JSON数组发送,这将是相似的,并且不会涉及很多额外的XML标签。 See converting an array in a json string into a list . 请参阅将json字符串中的数组转换为列表

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

相关问题 有没有更简单的方法来反序列化相似的XML文件? - Is there an easier way to deserialize similar XML files? 将此XML反序列化为对象的最佳方法 - Best way to deserialize this XML into an object 在C#中序列化和反序列化XML以便重用XML文件,模型类和UserControl的最佳方法 - Best way to serialize and deserialize XML in C# so as to reuse the XML file, model class, and UserControl Restful wcf Service错误“无法使用根名称反序列化XML主体 - Restful wcf Service error 'Unable to deserialize XML body with root name 在文件的两个带标签的行之间获取所有内容以便可以反序列化的最佳方法是什么? - What's the best way to get all the content in between two tagged lines of a file so that you can deserialize it? 格式化RESTful服务的DateTime的最佳方法? - The best way to format a DateTime for a RESTful service? 在 c# 中反序列化 xml 的最佳方法? - best way to deserialize xml in c#? 在数据库中表示“重复事件”的最佳方法是什么? - What is the best way to represent “Recurring Events” in database? 为 Web 服务生成动态 XML 的最佳方法是什么? - What's the Best way to Generate a Dynamic XML for Web Service? XML-有没有更简单的方法 - XML - Is there an easier way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM