Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中读取JSON块#_C#_.net_Json_Json.net - Fatal编程技术网

C# 如何在C中读取JSON块#

C# 如何在C中读取JSON块#,c#,.net,json,json.net,C#,.net,Json,Json.net,我收到了webRequest的回复,下面是代码 HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; using (var twitpicResponse = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))

我收到了webRequest的回复,下面是代码

HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;

using (var twitpicResponse = (HttpWebResponse)request.GetResponse())
{
    using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
    {
        JavaScriptSerializer js = new JavaScriptSerializer();

        var objText = reader.ReadToEnd();

        while (reader.EndOfStream)
        {
            objText = reader.ReadLine();

            MyObject myobj= (MyObject)js.Deserialize<MyObject>(objText);
        }
    }
}
我想逐个读取此JSON块(此JSON块具有多个值),并将其存储到objectlist中。

尝试:

using (var twitpicResponse = (HttpWebResponse)request.GetResponse())
{
   using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
   {
     JavaScriptSerializer js = new JavaScriptSerializer();
     var objText = reader.ReadToEnd();  
     MyObject myobj= (MyObject)js.Deserialize<MyObject>(objText);
   }
 }
使用(var twitpicResponse=(HttpWebResponse)request.GetResponse())
{
使用(var reader=newstreamreader(twitpicResponse.GetResponseStream())
{
JavaScriptSerializer js=新的JavaScriptSerializer();
var objText=reader.ReadToEnd();
MyObject myobj=(MyObject)js.反序列化(objText);
}
}
不需要while循环,因为您已经读取到流的末尾。 只要MyObject也可以从json解析,反序列化就可以了。

您可以使用以下代码将其读入一个C#对象:

该对象将是外部JSON对象的数组,如果MyObject与JSON结构匹配,则以下操作应有效:

var myobj = JsonConvert.DeserializeObject<MyObject>(JsonString);
var myobj=JsonConvert.DeserializeObject(JsonString);

您可以这样做,首先尝试在此处解析Json字符串\

如果它经过那里,那么就去。。。粘贴你的字符串。。。它将生成一个具有属性的类

然后

将类粘贴到代码中。并为类创建新对象

并调用反序列化

   JavaScriptSerializer js = new JavaScriptSerializer();
   var Data = DeserializeFromJson<RootObject>("Json String");
 public T DeserializeFromJson<T>(string json)
    {
        System.Web.Script.Serialization.JavaScriptSerializer ObjJSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        T deserializedProduct = ObjJSerializer.Deserialize<T>(json);
        return deserializedProduct;
    }


public class Result
{
public string Id { get; set; }
public string ModerationStatus { get; set; }
public string LastModificationTime { get; set; }
public bool IsRatingsOnly { get; set; }
public int TotalCommentCount { get; set; }
public int Rating { get; set; }
public int RatingRange { get; set; }
public bool IsRecommended { get; set; }
public int TotalFeedbackCount { get; set; }
public int TotalPositiveFeedbackCount { get; set; }
public int TotalNegativeFeedbackCount { get; set; }
public string DisplayLocale { get; set; }
public string SubmissionTime { get; set; }
public bool IsFeatured { get; set; }
public string LastModeratedTime { get; set; }
public string ProductId { get; set; }
public string AuthorId { get; set; }
 }

  public class Brand
  {
public string Id { get; set; }
public string Name { get; set; }
  }

  public class __invalid_type__3240234
 {
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string BrandExternalId { get; set; }
public Brand Brand { get; set; }
public string CategoryId { get; set; }
public string ProductPageUrl { get; set; }
   }

public class Products
{
public __invalid_type__3240234 __invalid_name__3240234 { get; set; }
 }

 public class Z7knrbjunvi022pe4swqp18fra
{
public string Id { get; set; }
public string ModerationStatus { get; set; }
}

 public class Zsknvekklaxl56nzwliultz5mp
{ 
public string Id { get; set; }
public string ModerationStatus { get; set; }
}

public class Authors
{
public Z7knrbjunvi022pe4swqp18fra z7knrbjunvi022pe4swqp18fra { get; set; }
public Zsknvekklaxl56nzwliultz5mp zsknvekklaxl56nzwliultz5mp { get; set; }
 }

 public class Includes
{
public Products Products { get; set; }
public Authors Authors { get; set; }
}

public class RootObject
{
public List<Result> Results { get; set; }
public Includes Includes { get; set; }
public bool HasErrors { get; set; }
public int Offset { get; set; }
public int Limit { get; set; }
public int TotalResults { get; set; }
}
JavaScriptSerializer js=新的JavaScriptSerializer();
var Data=DeserializeFromJson(“Json字符串”);
public T反序列化fromJSON(字符串json)
{
System.Web.Script.Serialization.JavaScriptSerializer ObjJSerializer=新的System.Web.Script.Serialization.JavaScriptSerializer();
T deserializedProduct=ObjJSerializer.Deserialize(json);
返回反序列化的产品;
}
公开课成绩
{
公共字符串Id{get;set;}
公共字符串调节状态{get;set;}
公共字符串LastModificationTime{get;set;}
公共布尔值仅为{get;set;}
公共int TotalCommentCount{get;set;}
公共整数评级{get;set;}
公共整数范围{get;set;}
公共布尔值建议为{get;set;}
公共int TotalFeedbackCount{get;set;}
公共整数TotalPositiveFeedbackCount{get;set;}
公共整数TotalNegativeFeedbackCount{get;set;}
公共字符串DisplayLocale{get;set;}
公共字符串提交时间{get;set;}
公共布尔值为{get;set;}
公共字符串LastModeratedTime{get;set;}
公共字符串ProductId{get;set;}
公共字符串AuthorId{get;set;}
}
大众品牌
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
}
公共类\uuu无效\u类型\uuuu 3240234
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共字符串BrandExternalId{get;set;}
公共品牌品牌{get;set;}
公共字符串CategoryId{get;set;}
公共字符串ProductPageUrl{get;set;}
}
公共类产品
{
公共uuu无效uu类型uuu 3240234 uuu无效uuu名称uuuu 3240234{get;set;}
}
公共级Z7knrbjunvi022pe4swqp18fra
{
公共字符串Id{get;set;}
公共字符串调节状态{get;set;}
}
公共类ZSKNVEKKLAX56NZWLIULTZ5MP
{ 
公共字符串Id{get;set;}
公共字符串调节状态{get;set;}
}
公共类作者
{
公共Z7KNRBJUNVI022PE4SWP18FRA Z7KNRBJUNVI022PE4SWP18FRA{get;set;}
公共Zsknvekklaxl56nzwliultz5mp Zsknvekklaxl56nzwliultz5mp{get;set;}
}
公共课包括
{
公共产品产品{get;set;}
公共作者{get;set;}
}
公共类根对象
{
公共列表结果{get;set;}
公共包含包括{get;set;}
公共bool hasrerrors{get;set;}
公共整数偏移量{get;set;}
公共整数限制{get;set;}
公共整数TotalResults{get;set;}
}

您正在使用“ReadToEnd();”,因此流光标现在位于流的末尾。然后检查“reader.EndOfStream”是否为真。它始终是“ReadToEnd();”的真正原因。在while循环中,还可以使用“reader.ReadLine();”来读取流的下一行。什么东西总是会返回一个空对象,因为你没有下一行要读……你的Json字符串格式错误!!这就是为什么反序列化如果不起作用。。将Json字符串粘贴到此处。。。我尝试了你的Json字符串。。它的表演errors@Aravind,Jsonlint结果:有效的JSON。我已经更新了我的答案,请查看…@Irshad。。它在我的代码中运行良好…:)如果它对你有用,而不是说谢谢,接受答案你试过了吗。。我试过JavaScript序列化程序和Newtonsoft。。两者都返回null。。我已经发布了我的代码,请查收it@Aravind我没有费劲创建MyObject模型,因为我没有时间,但是第一段代码(使用非泛型方法并返回一个对象)按预期工作,我可以通过放置断点并在Visual Studio中检查来验证这一点。@Jmk var x=JsonConvert.DeserializeObject(JsonString);这一个将起作用,因为您正在直接传递字符串。。。但我们需要一个解决方案,就像你提到的,通过传递对象来实现na。。。所以我只想谢谢你的回复
var myobj = JsonConvert.DeserializeObject<MyObject>(JsonString);
   JavaScriptSerializer js = new JavaScriptSerializer();
   var Data = DeserializeFromJson<RootObject>("Json String");
 public T DeserializeFromJson<T>(string json)
    {
        System.Web.Script.Serialization.JavaScriptSerializer ObjJSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        T deserializedProduct = ObjJSerializer.Deserialize<T>(json);
        return deserializedProduct;
    }


public class Result
{
public string Id { get; set; }
public string ModerationStatus { get; set; }
public string LastModificationTime { get; set; }
public bool IsRatingsOnly { get; set; }
public int TotalCommentCount { get; set; }
public int Rating { get; set; }
public int RatingRange { get; set; }
public bool IsRecommended { get; set; }
public int TotalFeedbackCount { get; set; }
public int TotalPositiveFeedbackCount { get; set; }
public int TotalNegativeFeedbackCount { get; set; }
public string DisplayLocale { get; set; }
public string SubmissionTime { get; set; }
public bool IsFeatured { get; set; }
public string LastModeratedTime { get; set; }
public string ProductId { get; set; }
public string AuthorId { get; set; }
 }

  public class Brand
  {
public string Id { get; set; }
public string Name { get; set; }
  }

  public class __invalid_type__3240234
 {
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string BrandExternalId { get; set; }
public Brand Brand { get; set; }
public string CategoryId { get; set; }
public string ProductPageUrl { get; set; }
   }

public class Products
{
public __invalid_type__3240234 __invalid_name__3240234 { get; set; }
 }

 public class Z7knrbjunvi022pe4swqp18fra
{
public string Id { get; set; }
public string ModerationStatus { get; set; }
}

 public class Zsknvekklaxl56nzwliultz5mp
{ 
public string Id { get; set; }
public string ModerationStatus { get; set; }
}

public class Authors
{
public Z7knrbjunvi022pe4swqp18fra z7knrbjunvi022pe4swqp18fra { get; set; }
public Zsknvekklaxl56nzwliultz5mp zsknvekklaxl56nzwliultz5mp { get; set; }
 }

 public class Includes
{
public Products Products { get; set; }
public Authors Authors { get; set; }
}

public class RootObject
{
public List<Result> Results { get; set; }
public Includes Includes { get; set; }
public bool HasErrors { get; set; }
public int Offset { get; set; }
public int Limit { get; set; }
public int TotalResults { get; set; }
}