我想知道在 C# 中解析 JSON 的最有效方法是什么?我所说的高效是指响应时间较短的那个。我正在尝试使用几种方法解析大量数据,并且这两种方法的响应时间都很长。谁能告诉我以下方法之间的区别?是否有替代方法可以让我以较短的响应时间进行解析?
选项1:
HttpWebRequest request = WebRequest.Create(jsonURL) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(obj));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
}
选项 2:
var json = new WebClient().DownloadString(jsonURL);
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(obj));
object objResponse = jsonSerializer.ReadObject(ms);
}