我正在尝试解析一个JSON字符串,看起来像这样:
{
"totalCreditsRemoved": 1,
"invalidReceivers": [],
"ids": [100070531],
"validReceivers": ["+33635938286"]
}
我从 Web API 中检索它,并将其存储为字符串:
var reader = new StreamReader(respStream);
String result = reader.ReadToEnd().Trim();
response = result;
响应是公共字符串
然后,在另一种方法中:我尝试解析我的 json 字符串:
var json = response;
var objects = JArray.Parse(json);
foreach (JObject root in objects)
{
foreach (KeyValuePair<String, JToken> app in root)
{
totalCreditsRemoved = (String)app.Value["totalCreditsRemoved"];
invalidReceivers = (String)app.Value["invalidReceivers"];
ids = (String)app.Value["ids"];
validReceivers = (String)app.Value["validReceivers"];
}
}
但我总是收到此错误:
JsonReaderException:从 JsonReader 读取 JArray 时出错。当前 JsonReader 项不是一个数组:StartObject。路径 '',第 1 行,位置 1。
错误发生在以下行:
var objects = JArray.Parse(json);
我不明白如何解决这个问题?OVHjson不是已经是一个数组了吗提前感谢!
不需要JArray.Parse,因为它不是一个数组。并且做得过分了..因此错误..
var objects = JObject.Parse(json);
会做这项工作
并提取
totalCreditsRemoved = (String)objects.Value["totalCreditsRemoved"];
invalidReceivers = (String)objects.Value["invalidReceivers"];
ids = (String)objects.Value["ids"];
validReceivers = (String)objects.Value["validReceivers"];
dynamic jsonObj = JsonConvert.DeserializeObject(responseFromServer);
foreach (var obj in obj1.Properties())
{
if (obj.Name == "rewardConfirmation") {
foreach (var obj2 in obj.Value)
{
MessageBox.Show(obj2.ToString());
}
}
}