在 C# 中解析 JSON API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10507416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing JSON API in C#
提问by Cistoran
so I'm fairly new to programming but am looking to go much deeper with it. I recently started to get involved in a project to create a WinForm program for a website that uses an API system in JSON.
所以我对编程还很陌生,但我希望更深入地了解它。我最近开始参与一个项目,为一个使用 JSON 格式的 API 系统的网站创建一个 WinForm 程序。
I've never used an API before so I'm not quite sure how it works but after looking at it for a few minutes I seem to have the gist of it. What I don't get is how exactly parsing JSON in C# works.
我以前从未使用过 API,所以我不太确定它是如何工作的,但在看了几分钟之后我似乎明白了它的要点。我不明白的是在 C# 中解析 JSON 是如何工作的。
I found this link after a little google searching. And I got it working (somewhat) with this code.
我在谷歌搜索后找到了 这个链接。我用这段代码让它工作(有点)。
static void Main(string[] args)
{
WebClient c = new WebClient();
var vLogin = c.DownloadString("https://www.openraid.us/index.php/api/login/username/password");
//Returns string
//{"status":1,"error":null,"token":"250-1336515541-c48d354d96e06d488d1a2530071ef07c9532da26"}
//Token = random, no decisive length*/
JObject o = JObject.Parse(vLogin);
Console.WriteLine("Login Status: " + o["status"]);
String sToken = "" + o["token"];
Console.WriteLine(sToken);
Console.WriteLine("");
//Breaks after this
var myRaids = c.DownloadString("https://www.openraid.us/index.php/api/myraids/"+sToken);
JObject r = JObject.Parse(myRaids); //error occurs here
String sEventId = "" + r["event_id"];
Console.WriteLine("Event ID: " + sEventId);
Console.ReadLine();
}
So to me it looks like I have parsing 1 page done and handled, but when I move onto the second I get this error.
所以对我来说,看起来我已经完成并处理了 1 个页面,但是当我进入第二个页面时,我收到了这个错误。
Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
从 JsonReader 读取 JObject 时出错。当前 JsonReader 项不是对象:StartArray。路径 '',第 1 行,位置 1。
So I guess my question is, how do I parse more than 1 page or call of JSON and what would be the easiest way to break up each section of the JSON object (Such as status, error, and token, into C# strings?
所以我想我的问题是,我该如何解析超过1页或JSON的电话,这将是打破了JSON对象的每个部分(如最简单的方法status,error和token,到C#字符串?
采纳答案by sjokko
Did you try to JArray instead? Depending on what kind of object you are trying to return
您是否尝试使用 JArray 代替?取决于您尝试返回的对象类型
WebClient client = new WebClient();
var data = client.DownloadString("");
var jArray = JArray.Parse(data);
回答by Ibraheem Osama
You can cut every JSON object(Array) into more object using for loops the C# API is System.Json
您可以使用 for 循环将每个 JSON 对象(数组)切割成更多对象,C# API 是 System.Json
var jsonArray = JsonArray.Parse(st);//st is the string which contain the JSON objects
foreach (var item in jsonArray) {
JsonObject ob = new JsonObject(item);
foreach (var t in ob.Values) {
JsonObject oo = new JsonObject(t);
foreach (var x in oo) {
textBox1.AppendText(x.Key + “ : ” + x.Value + “\n”);
}
}
}
回答by Kyle
JSON requires brackets for arrays and commas between multiple objects.
JSON 需要括号用于数组和多个对象之间的逗号。
This is per the JSON standard. I also recommend using JSON.net via NuGetinstead of the native JSON parser unless it is overkill and you cannot have the extra bloat.
这是根据 JSON 标准。我还建议通过 NuGet使用JSON.net而不是本机 JSON 解析器,除非它是矫枉过正并且你不能有额外的膨胀。
For example, your parsing a file with two seperate JSON objects - the following does not work per the JSON standard (lacks a comma between the 2 objects and the two objects are not encapsulated by brackets):
例如,您使用两个单独的 JSON 对象解析文件 - 以下不符合 JSON 标准(两个对象之间缺少逗号,并且两个对象未用括号封装):
{"status":1,"error":null}
{"status":2,"error":null}
The following 3 JSON objects parsed from a file does work (has brackets for multiple objects and commas between objects):
从文件解析的以下 3 个 JSON 对象确实有效(多个对象用括号表示,对象之间用逗号):
[{"glossary": {"title": "fun glossary","SeeAlso": ["GML", "XML"]},
{"glossary": {"title": "grey glossary","SeeAlso": ["GML", "XML"]},
{"glossary": {"title": "blue glossary","SeeAlso": ["GML", "XML"]}]

