我是
JSON.NET的新手,我正在尝试将JSON字符串反序列化为一个简单的.NET对象.
这是我的代码片段:
这是我的代码片段:
当我做
- MessageBox.Show(deserializedPost.titre);
我总是得到这个错误:
Value can not be null.
这是我的对象,我想填充检索到的JSON元素:
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- namespace MeltyGeeks
- {
- public class Post
- {
- public String titre { get; set; }
- public String aresum { get; set; }
- // Constructor
- public Post()
- {
- }
- }
- }
这是我的JSON字符串的片段:
- {"root_tab":{"tab_actu_fil":{"data":[{"c_origine":"MyApp","titre":"title of first article","aresum":"this is my first Article
- "tab_medias":{"order":{"810710":{"id_media":810710,"type":"article","format":"image","height":138,"width":300,"status":null}}}},
您显示的JSON结构与Post对象不匹配.您可以定义其他类来表示此结构:
- public class Root
- {
- public RootTab root_tab { get; set; }
- }
- public class RootTab
- {
- public ActuFil tab_actu_fil { get; set; }
- }
- public class ActuFil
- {
- public Post[] Data { get; set; }
- }
- public class Post
- {
- public String titre { get; set; }
- public String aresum { get; set; }
- }
然后:
或者如果您不想定义这些额外的对象,您可以这样做:
- var root = JsonConvert.DeserializeObject<JObject>(Feed);
- Post[] posts = root["root_tab"]["tab_actu_fil"]["data"].ToObject<Post[]>();
- string titre = posts[0].titre;