2 u010650772 u010650772 于 2013.08.27 20:34 提问

在wp7上用JSON.NET反序列化json串时遇到了问题

在手机端获得了这样的json串:
[
{"class":"Board","id":1,"articles": [{"class":"Article","id":1}],
"boardName":"122222","boarder": {"class":"User","id":1},
"description":"22222","postCount":0},
{"class":"Board","id":2,"articles":[],"boardName":"555555","boarder":{"class":"User","id":2},"description":"555555","postCount":0}]

我打算用json.NET来解析它。已经把Newtonsoft.Json.dll添加进来。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
然后我写了一系列的类(这里我想用Board这个类来接收json串):

//为接收 Board json 串写的两个类
public class Boarder
{
public string @class { get; set; }
public int id { get; set; }
}
public class Board
{
public string @class { get; set; }
public int id { get; set; }
public List articles { get; set; }
public string boardName { get; set; }
// public Boarder boarder { get; set; }
public User boarder { get; set; }
public string description { get; set; }
public int postCount { get; set; }
}

//为接收 Article json 串写的三个类
/*public class Board
{
public string @class { get; set; }
public int id { get; set; }
}*/
/*public class Owner
{
public string @class { get; set; }
public int id { get; set; }
}*/
public class Article
{
public string @class { get; set; }
public int id { get; set; }
public Board board { get; set; }
public List comments { get; set; }
public string detail { get; set; }
public string lastReplyTime { get; set; }
//public Owner owner { get; set; }
public User owner { get; set; }
public string postTime { get; set; }
public int replyCount { get; set; }
public string title { get; set; }
}

//为接收 User json 串写的三个类
/*public class Article
{
public string @class { get; set; }
public int id { get; set; }
}*/
/*public class Comment
{
public string @class { get; set; }
public int id { get; set; }
}*/
public class User
{
public string @class { get; set; }
public int id { get; set; }
public string address { get; set; }
public List articles { get; set; }
public object birthday { get; set; }
public List comments { get; set; }
public string email { get; set; }
public string lastLoginTime { get; set; }
public string nickName { get; set; }
public string password { get; set; }
public string picture { get; set; }
public string qq { get; set; }
public string registerTime { get; set; }
public string sex { get; set; }
public string signDetail { get; set; }
}

//为接收 Comment json 串写的三个类
/*public class Article
{
public string @class { get; set; }
public int id { get; set; }
}*/
/*public class Owner
{
public string @class { get; set; }
public int id { get; set; }
}*/
public class Comment
{
public string @class { get; set; }
public int id { get; set; }
public Article article { get; set; }
public string detail { get; set; }
//public Owner owner { get; set; }
public User owner { get; set; }
public string postTime { get; set; }
}

接收类写完后,我用Board类来接收json串:
List p1 = JsonConvert.DeserializeObject>(e.Result.ToString()); //获取到的json数据反序列化

结果程序报错,错误信息为:
“Newtonsoft.Json.JsonConvert.DefaultSettings 未能计算表达式的值 System.Func"
"emptyArray = 有关包含类的信息不可用,因此无法获取字段“_emptyArray”的值。"
~ ~

求各位大神帮我看看我写的类是不是有问题?谢谢!

3个回答

jkuco
jkuco   2013.08.28 14:10
已采纳

在jsp中有个eval的,不知道可以运用不,用了次标签可以还原到开始的json状态,具体的我不知道这个东西怎么用。你看看,或许可以有收获。

chenyi2821267
chenyi2821267   2013.08.27 23:52

如果你的json含有嵌套,定义的class里子json项设为自定义类型可能会出错,可以试试用将子json视为string类型public string XXX{ get; set; },然后在set里将嵌套的json取出再解析一次

u013125604
u013125604   2015.08.14 21:09

对方规划即可吗妈妈能金鹤范第四中学风风光光图片

Csdn user default icon
上传中...
上传图片
插入图片