[英]Invalid JSON primitive: System.Net.ConnectStream
I'm trying to deserialize 我正在尝试反序列化
string strurlTest = String.Format("https://jsonplaceholder.typicode.com/posts/1/comments");
WebRequest requestObjGet = WebRequest.Create(strurlTest);
HttpWebResponse responseObjGet = null;
responseObjGet = (HttpWebResponse)requestObjGet.GetResponse();
string strresulttest = null;
JavaScriptSerializer js = new JavaScriptSerializer();
using (Stream stream = responseObjGet.GetResponseStream())
{
StreamReader sr = new StreamReader(stream);
strresulttest = sr.ReadToEnd();
test t = new JavaScriptSerializer().Deserialize<dynamic>(res.ToString());
then I receive the error 然后我收到错误
"Invalid Json Primitive" “无效的Json原语”
please your support on it. 请你的支持。 Json data https://jsonplaceholder.typicode.com/posts/1/comments Json数据https://jsonplaceholder.typicode.com/posts/1/comments
Test[] tests= new JavaScriptSerializer().Deserialize<Test[]>(strresulttest );
use strresulttest instead of res. 使用strresulttest而不是res。 So your 'test' class should be like 所以你的'测试'课应该是这样的
public class Test
{
public int postId { get; set; }
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string body { get; set; }
}
Working solution as per comment 根据评论工作解决方案
using System;
using System.IO;
using System.Net;
using System.Web.Mvc;
using System.Web.Script.Serialization;
namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
string strurlTest = String.Format("https://jsonplaceholder.typicode.com/posts/1/comments");
WebRequest requestObjGet = WebRequest.Create(strurlTest);
HttpWebResponse responseObjGet = null;
responseObjGet = (HttpWebResponse)requestObjGet.GetResponse();
string strresulttest = null;
JavaScriptSerializer js = new JavaScriptSerializer();
using (Stream stream = responseObjGet.GetResponseStream())
{
StreamReader sr = new StreamReader(stream);
strresulttest = sr.ReadToEnd();
Test[] tests = new JavaScriptSerializer().Deserialize<Test[]>(strresulttest);
}
return View();
}
}
public class Test
{
public int postId { get; set; }
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string body { get; set; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.