简体   繁体   English

尝试使用C#从URL解析JSON

[英]Trying to parse JSON from a url using C#

I have an exe file I wrote in .NET which I want to read a .json file from a URI, and return a value in string format. 我有一个用.NET编写的exe文件,我想从URI中读取.json文件,并以字符串格式返回值。 I am pretty new to .NET and c# but can easily do this in PowerShell (using Invoke-RestMethod). 我对.NET和c#相当陌生,但是可以在PowerShell中轻松地做到这一点(使用Invoke-RestMethod)。 I don't want to use an external library such as json.net (would like to distribute this as a single .exe). 我不想使用外部库,例如json.net(想将其作为单个.exe分发)。 The only thing I found so far was System.Runtime.Serialization.Json - which looks like it will work but also seems extremely complex and difficult to use. 到目前为止,我发现的唯一东西是System.Runtime.Serialization.Json-看起来可以工作,但看起来也非常复杂且难以使用。 Is this the library I should be using to perform this simple task? 这是我应该用来执行此简单任务的库吗? Is there an easier way to do this? 有没有更简单的方法可以做到这一点?

导入System.Web.Script.Serialization,然后使用JavaScriptSerializer对象。

System.Runtime.Serialization.Json really isn't all that bad. System.Runtime.Serialization.Json确实还不错。 Yes, it is the library you should use if you don't want to go third-party. 是的,如果您不想成为第三方,则应使用该库。

The exception to this is if your JSON is trivial. 如果您的JSON不重要,则例外。 If it's trivial, just do the string parsing yourself. 如果比较琐碎,只需对字符串进行自我分析即可。

If you don't want to parse your own JSON I think the System.Runtime.Serialization.Json namespace is your best bet without using an external library.. 如果您不想解析自己的JSON,那么我认为System.Runtime.Serialization.Json命名空间是最好的选择,而无需使用外部库。

JSON.NET is the best if you can use an external library though JSON.NET是最好的,如果您可以使用外部库

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

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