我在ASP中遇到了一个小问题.NET 4,使用C#.我正在try 创建一个前端GUI,通过多个Web API发送和接收数据.拥有多个API的原因与我们的网络有关,它由几个安全区域组成.服务器位于不同的区域,一个简单的请求可能需要通过多达3个不同的API.

Specifically, I'm sending a JSON object from the GUI to the first API. The JSON object is supposed to be forwarded to the next API and the next - and from there a new JSON object is created and returned down the same path.

这条路本身运行良好.问题是,一旦JSON对象返回到GUI,就无法对其进行解析.我收到一个用引号括起来的JSON字符串,每个API对应一次.

字符串的开头可能如下所示:

Hey! I am a string, or a JSON object of sorts!

API之间的第一个 skip 给了我:

"Hey! I am a string, or a JSON object of sorts!"

After the next hop it looks like this:

"\"Hey! I am a string, or a JSON object of sorts!\""

当我的GUI掌握它时,我们就有了类似的东西:

"\"\\\"Hey! I am a string, or a JSON object of sorts!\\\"\""

This is where parsing fails, for obvious reasons. The JSON object itself is properly formatted, but all the quotes are causing problems for the JSON.net parser (all quotes inside the object are also wrapped multiple times).

到目前为止,我try 的是将请求作为application/json类型和text/Plain类型发送.两个人都做了同样的事情.API返回HttpResponseMessage,使用ReadAsStringAsync()读取.我还试图避免字符串读取,只是直接从HttpRequestMessage读取到HttpResponseMessage,并且只在GUI中执行ReadAsStringAsync(),但问题仍然存在.使用JSON.net Serialize()-method创建JSON字符串,并使用StringContent()将其放入HttpContent.这似乎正确地完成了工作.我相信引号是在API和GUI接收到HttpResponseMessage时生成的.

Any idea how I can send and receive the JSON string as a raw string, that is not treated in any way?

我可以通过在每个API上将对象解析为JToken或JObject,然后再次序列化来绕过这种行为.然而,这并不是一个好的解决方案——我宁愿API只是按照他们获得消息的方式转发消息,而不做任何处理.我研究了使用路由的转发,但是有很多授权的东西,这确实需要我使用API操作,而不是重定向路由.

To clarify (TLDR): The ideal solution would be for the API's to simply pass along the message without parsing or reading anything into it. As long as the source of the message is authorized, the request is encrypted and the requested URL is valid, the message itself doesn't matter much to each of the "proxy" API's.

推荐答案

After MUCH research, I finally figured this one out.

首先;我直接返回了HttpResponseMessage;我并不是故意在API路径的每个跃点中反序列化它.

The problem, as it turns out, was actually that we were using a mix of the "native" MVC serialization methods and JSON.net's methods. Either one by itself is fine, and provides a clean passthrough of all API's. If, however, we would combine serialized data from both native methods and JSON.net-methods, the API's further down the chain would not be able to recognize the formatting and incorrectly assume that the content should be serialized again (using native methods).

所以解决方案就是删除所有JSON.net方法,我们最终得到了预期的结果.

Json相关问答推荐

如何在Power BI中集成API和JSON数据后将数据转换为表?

在PowerShell中,如何获取数据对所在的JSON对象的名称

Golang返回的JSON顶级字段是可变的.如何在 struct 中使用

集成wix.comstore API|变音符号问题

Pandas 对REST API的自定义响应

从包含 JSON 对象序列的文件中获取第一个 JSON 对象

如何使用 jq 将字符串数组转换为对象?

jq - 将父键值提取为子元素旁边的逗号分隔值

JSONPath:查找子项目条件在字符串列表中的项目

N1QL 搜索对象内的某些对象

C# 合并 2 个几乎相同的 JSON 对象

Json.NET SerializeObject 转义值以防止 XSS

如何判断 Json 对象中是否存在键并获取其值

SyntaxError:Object.parse(本机)AngularJS中的意外标记o

在 JavaScript 中从 Json 数据中删除反斜杠

android - 在 adb logcat 输出中格式化 json 字符串

Jackson 动态属性名称

JSON 到 JSON 转换器

ASP.NET MVC 读取原始 JSON 发布数据

在android中读取Json数组