简体   繁体   English

从ASP.Net MVC控制器中的第三方API返回JSON字符串

[英]Returning JSON string from 3rd party API in ASP.Net MVC controller

I'm wrapping a third-party API in a controller (due to security hoops we have to jump through) and the API returns a JSON string. 我在控制器中包装了第三方API(由于必须跳过安全环),该API返回JSON字符串。

I do some minor changes to the JSON string and want to return that string. 我对JSON字符串做了一些小的更改,并希望返回该字符串。

I don't see a way to do that with a JSONResult, as it requires an object, and returning JSON string is sent back as a string. 我没有用JSONResult做到这一点的方法,因为它需要一个对象,并且返回的JSON字符串将作为字符串发送回去。

Am I stuck with using something like a ContentResult? 我是否坚持使用诸如ContentResult之类的东西?

JSONLint.com says the modified JSON is valid. JSONLint.com说修改后的JSON有效。 It starts with... 它从...开始

[{"Acknowledgment Start Date":null,"Additional Location Details":null,"Area Code":null,"Assign To Vendor":"No",...

If I use the Newtonsoft.Json.JsonConvert(), it does this to my JSON string... 如果我使用Newtonsoft.Json.JsonConvert(),它将对我的JSON字符串执行此操作...

[[[[]],[[]],[[]],[[]],[[]],[[]],[[]],[[]],...

If I use JavaScriptSerializer, I get this again... 如果我使用JavaScriptSerializer,我会再次得到这个...

[[[[]],[[]],[[]],[[]],[[]],[[]],[[]],[[]],...

I suspect part of the problem is the null values in the JSON string. 我怀疑问题的一部分是JSON字符串中的空值。

Is there another solution? 还有其他解决方案吗? Are there issues with using a ContentResult that I'm not aware of? 使用我不知道的ContentResult是否有问题?

Newtonsoft.Json.JsonConvert() serializes an object, it doesn't parse (AKA deserialize) it. Newtonsoft.Json.JsonConvert() 序列化一个对象,但不解析 (也称为反序列化)该对象。 What you're doing when you call Newtonsoft.Json.JsonConvert(jsonString) is you're saying "serialize this JSON string to JSON". 当您调用Newtonsoft.Json.JsonConvert(jsonString) ,您正在说的是“将此JSON字符串序列化为JSON”。 So, you get a funky result. 因此,您得到一个时髦的结果。

You could instead parse the JSON, then make your modifications, then serialize it again. 您可以解析 JSON,然后进行修改,然后再次进行序列化 For example: 例如:

var myObject = Newtonsoft.Json.DeserializeObject<POCOClass>(jsonString);
myObject.Whatever = "123";
//... etc.

This of course after defining your POCO class like such: 当然,在定义了POCO类之后,这是这样的:

public class POCOClass {
    [JsonProperty(PropertyName = "Acknowledgment Start Date")]
    public string AcknowledgmentStartDate { get; set; }
    // etc.
}

Then when you're done, serialize it back: 然后,完成后,将其序列化回:

jsonString = Newtonsoft.Json.JsonConvert(myObject) // or Newtonsoft.Json.SerializeObject(myObject)

Upon closer inspection, I had a couple of realizations. 经过仔细检查,我有了一些认识。

The first being that the outputted JSON isn't valid because the quoted identifiers include spaces and colons. 第一个是输出的JSON无效,因为引用的标识符包含空格和冒号。 For example, this one from above. 例如,这个从上面。

... ,"Additional Location Details":null, ...

While I suspect this is the root of problems, I don't have the time to write a parser to make it proper JSON. 虽然我怀疑这是问题的根源,但我没有时间编写解析器以使其适合JSON。

I will come back and write a tool to properly clean up the mess at some point, but right now I have another approach I'm doing. 我会回来并写一个工具来适当地清理混乱,但是现在我正在使用另一种方法。

暂无
暂无

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

相关问题 在ASP.NET MVC中管理对第三方应用程序的共享API调用的最佳场所 - Best Place to manage shared API calls to 3rd party application inside my asp.net mvc ASP.NET MVC 5:如何从 3rd 方库中禁用自定义错误页面 - ASP.NET MVC 5: How to disable custom error page from 3rd party library 从控制器返回Json数据以查看ASP.NET MVC - Returning Json Data From Controller to View ASP.NET MVC 将 JSON 结果返回到 API 调用,来自 Z9E0DA8438E1E38A1C30F4B576CE7 中已有的 Controller 操作 - Returning JSON results to API calls from already existing Controller actions in ASP.NET MVC 5 在 ASP.NET MVC 中重定向到第 3 方 URL 后如何维护会话? - How to maintain session after redirection to 3rd party URL in ASP.NET MVC? ASP.NET Core Web API - 如何使用 HttpClient 使用 3rd 方 API - ASP.NET Core Web API - How to Consume 3rd party API using HttpClient 如何在第三方ASP.NET Web API客户端中使用Oauth生成的令牌? - How to use an Oauth Generated Token in a 3rd party asp.net web API client? 如何在asp.net core web api中实现JWT Refresh Tokens(没有第三方)? - How to implement JWT Refresh Tokens in asp.net core web api (no 3rd party)? 如何在不等待 ASP.NET API 中的响应的情况下处理多个 3rd 方 API - How to process multiple 3rd party APIs without waiting for their response in ASP.NET API 我如何验证第三方用户访问我的Web API asp.net? - how do i authenticate 3rd party user to access my web API asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM