且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Twitter api json格式数据分配给C#中的类字段?

更新时间:2023-02-09 15:16:23

[在json的开头指示一个数组,所以尝试反序列化为对象的列表

  var  objs = JsonConvert.DeserializeObject&lt ; List< Trendingcloset>>(serobj); 


本文详细介绍了JSON。还有一个Twitter示例:在C#中使用JSON& VB [ ^ ]

i got stuck here ,please help me out

What I have tried:

i got "[{"name":"Pune","placeType":{"code":7,"name":"Town"},"url":"http:\/\/where.yahooapis.com\/v1\/place\/2295412","parentid":23424848,"country":"India","woeid":2295412,"countryCode":"IN"}]" data from twitter api.
now i have created cs class for the same,so how do i assign this data dynamically to my class fields ?

<pre>dynamic serobj = JsonConvert.SerializeObject(obj);
            var objs = JsonConvert.DeserializeObject<Trendingcloset>(serobj);


public string name { get; set; }
      public string placeType { get; set; }
      public int code { get; set; }
      public string placename { get; set; }
      public string url { get; set; }
      public int parentid { get; set; }
      public string country { get; set; }
      public int woeid { get; set; }
      public string countryCode { get; set; }

The [ at the start of the json indicates an array so try to deserialize into a List of your object :
var objs = JsonConvert.DeserializeObject<List<Trendingcloset>>(serobj);


This article covers JSON in detail. There is also a Twitter example: Working with JSON in C# & VB[^]