【发布时间】:2018-07-30 22:17:47
【问题描述】:
我在 C# 中有一个从 PHP 页面下载 JSON 对象的函数,只要它只包含一条记录,它就可以正常工作。我正在将 JSON 读入字典。
using (WebClient webClient = new System.Net.WebClient())
{
WebClient n = new WebClient();
var json = n.DownloadString("http://127.0.0.1/testjson.php");
Dictionary<string, string> htmlAttributes = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
int ID = Int32.Parse(htmlAttributes["id"]);
string Phone = htmlAttributes["phone"];
string Message = htmlAttributes["message"];
string UID = htmlAttributes["uid"];
Console.WriteLine(ID);
Console.WriteLine(Phone);
Console.WriteLine(Message);
Console.WriteLine(UID);
}
只要我使用如下 JSON 对象,它就可以正常工作:
{"id":26333,"phone":"+46734231233","message":"Testmeddelande","uid":"A774059B-69CE-40D7-809B-C0A63728F5B9"}
但是当我想解析一个以上的记录时,我得到了错误 - 我得到的错误是:
Newtonsoft.Json.JsonReaderException: '解析值时遇到意外字符:[.路径“数据”,第 1 行,位置 9。'
JSON 对象看起来像这样,我已经在线验证了它:
{"data":[{"id":26333,"phone":"+46734231233","message":"Testmeddelande","uid":"A774059B-69CE-40D7-809B-C0A63728F5B9"},{"id":26333,"phone":"+46734231233","message":"Wow, det fungerar","uid":"CA528FCA-CD5D-4220-A646-B556FC060550"}]}
我是新手,所以解决方案可能很明显?
最好的问候,乔金姆
【问题讨论】:
标签: c# php json dictionary json.net