从C#产生JSON:WebMessageBodyStyle.Wrapped或WebMessageBodyStyle.Bare? [英] Producing JSON from C#: WebMessageBodyStyle.Wrapped or WebMessageBodyStyle.Bare?

查看:521
本文介绍了从C#产生JSON:WebMessageBodyStyle.Wrapped或WebMessageBodyStyle.Bare?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C ++ REST SDK库编写一个C ++应用程序,该程序将处理C#应用程序生成的JSON数据. C#程序可以产生包装"或裸露"样式的JSON.

I am trying to write a C++ application, using C++ REST SDK lib, that will process JSON data produced by a C# application. A C# program can produce JSON in a "wrapped" or "bare" style.

使用BodyStyle = WebMessageBodyStyle.Wrapped,C#生成如下所示的JSON:

Using BodyStyle = WebMessageBodyStyle.Wrapped, C# produces JSON like the following:

{"Echo":"{\"firstname\":\"an'",\"number\":21,\"secondname\":\"pn\"}"}

使用BodyStyle = WebMessageBodyStyle.Bare,C#生成如下JSON:

Using BodyStyle = WebMessageBodyStyle.Bare, C# produces JSON like this:

"{\"firstname\":\"an'",\"number\":21,\"secondname\":\"pn\"}"

我的程序如何识别产生的类型:包裹裸露?

How can my program recognize which type was produced: Wrapped or Bare?

推荐答案

JSON是用于表示和交换数据的标准格式.它没有定义术语 Wrapped Bare .我不熟悉C#及其将数据编码为JSON的库,但是我可以根据您提供的示例进行猜测.

JSON is a standard format for representing, and exchanging, data. It does not define the terms Wrapped or Bare. I am not familiar with C# and its libraries for encoding data as JSON, however I can make a guess based on the samples you provided.

如果您可以控制C#应用程序,请将其编码为仅使用 Bare .通常,我认为 Wrapped 样式没有任何优势.也许它是专门为其他C#客户端库设计的.

If you have control over the C# application, code it to use Bare only. I see no advantage, in general, to the Wrapped style. Perhaps it is designed specifically for some other C# client libraries.

我在生成的输出中看到的唯一区别是数据的结构.不能绝对确定,但是从这两个示例中,您可以简单地查看反序列化的对象并检查其是否具有属性Echo.如果是这样,请使用该属性的值,如果不使用,请按原样使用对象.

The only difference I see in the produced output is the structure of the data. There is no way to be absolutely certain, but from those two samples you can simply look at the deserialized object and check if it has an attribute Echo. If it does, use the value of that attribute and if it doesn't then use the object as-is.

由于我已经十年没有使用C ++了,而且我不知道您正在使用的JSON库,因此我将在JavaScript中给出一个示例(尽管使用的样式可能更接近C ++).这是如何处理这两个对象的方法:

Since I haven't worked in C++ in over a decade and I do not know the JSON library you are using, I will give an example in JavaScript (though using a style that may be somewhat closer to C++). Here is how those two objects could be handled:

var data = JSON.parse(...); // the '...' represents where ever you get the text
if (data["Echo"] !== undefined)
    { data = data["Echo"]; }
console.log("The first name is:", data["firstname"]);

这是一个伪代码示例,几乎是有效的Java,可能更容易识别并转换为C ++:

Here is a psuedo-code example that is almost valid Java which may be more easily recognized and translated to C++:

Map<String, Object> data = JSON.parse(...); // the '...' represents where ever you get the text
if (data.containsKey("Echo"))
    { data = (Map)data.get("Echo"); }
System.out.println("The first name is: " + data.get("firstname"));

这篇关于从C#产生JSON:WebMessageBodyStyle.Wrapped或WebMessageBodyStyle.Bare?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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