目前我正在使用网页请求获取严格来自json的网页正文。
var request = WebRequest.Create("urihere");
string text;
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
现在我已经在一个变量中得到了json字符串,我正试图弄清楚如何解析这些数据。我正在尝试使用json.net,如下所示:
User primaryuser = new Models.User();
primaryuser.FullName = "Test";
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
using (StreamWriter sw = new StreamWriter("filenamehere"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, primaryuser);
}
当我尝试将uri传递给流式编写器时,我得到了一个“uri not supported”错误。
使用json.net从uri映射此数据的正确方法是什么?
答案 0 :(得分:2)
在Json.net中序列化和反序列化对象:
string json = JsonConvert.SerializeObject(primaryuser, Formatting.Indented);
User primaryuser = JsonConvert.DeserializeObject<User>(json);
http://www.newtonsoft.com/json/help/html/SerializeObject.htm http://www.newtonsoft.com/json/help/html/DeserializeObject.htm