【问题标题】:how to convert json to local database class property如何将json转换为本地数据库类属性
【发布时间】:2014-02-04 09:54:09
【问题描述】:

我遇到了将 json 值转换为本地数据库类的问题 如果我使用 list 代替 EntitySet

,它工作正常

这是我的类属性

当我从代码转换下面的类时

HttpClient client = new HttpClient();
var data = await client.GetStringAsync("http://demo.com/wk/lg/initimeiNumber=911115000013916&tz=Asia/Kolkata");
Info_Entity = JsonConvert.DeserializeObject<Info>(data);

它给出错误:

无法反序列化当前 JSON 对象(例如 {"name":"value"}) 进入类型 'System.Data.Linq.EntitySet`1[ViewModel.Result]' 因为 type 需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化。到 修复此错误或者将 JSON 更改为 JSON 数组(例如 [1,2,3]) 或更改反序列化类型,使其成为普通的 .NET 类型(例如 不是像整数这样的原始类型,不是像数组这样的集合类型 或列表)可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型中以强制它 从 JSON 对象反序列化。路径“result.taskarray”,第 3 行, 位置 17。

        [Table]
public class Detail
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int id { get; set; }

    [Column]
    public string MobileNo { get; set; }
    private EntityRef<DetailInfo> _DetailInfo;
    [Association(Storage = "_DetailInfo", ThisKey = "id", OtherKey = "Did", IsForeignKey = true)]
    public DetailInfo DetailInfo
    {
        get { return _DetailInfo.Entity; }
        set
        {
            _DetailInfo.Entity = value;
        }
    }
}

[Table]
public class DetailInfo
{     
    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int Did   {  get;     set;  }
    [Column]
    public string phoneno { get; set; }
    [Column]
    public string address { get; set; }

    private EntityRef<Info> _info;
    [Association(Storage = "_info", ThisKey = "Did", OtherKey = "Id", IsForeignKey = true)]
    public Info Info
    {
        get { return _info.Entity; }
        set { _info.Entity = value;}
    }

    private EntitySet<Detail> _Detail;
    [Association(Storage = "_Detail", OtherKey = "id", ThisKey = "Did")]
    public EntitySet<Detail> Detail
    {
        get { return this. _Detail; }
        set { this._Detail.Assign(value); }
    }
    public DetailInfo()
    {
        _Detail = new EntitySet<Detail>(
            new Action<Detail>(this.attach_ToDo),
            new Action<Detail>(this.detach_ToDo)
            );
    }


    private void attach_ToDo(Detail Dinfo)
    {
              Dinfo.DetailInfo = this;
    }

    // Called during a remove operation
    private void detach_ToDo(Detail Dinfo)
    {       
        Dinfo.DetailInfo = this;
    }

}

[Table]
public class Info
{

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int Id { get; set; }
    [Column]
    public string firstName { get; set; }
    [Column]
    public string lastName { get; set; }
    [Column]
    public string imageUrl { get; set; }
    private EntitySet<DetailInfo> _DetailInfo;
    [Association(Storage = "_DetailInfo", OtherKey = "Did", ThisKey = "Id")]
    public EntitySet<DetailInfo> DetailInfo
    {
        get { return this._DetailInfo; }
        set { this._DetailInfo.Assign(value); }
    }
    public Info()
    {
        _DetailInfo = new EntitySet<DetailInfo>(
            new Action<DetailInfo>(this.attach_ToDo),
            new Action<DetailInfo>(this.detach_ToDo)
            );
    }
       private void attach_ToDo(DetailInfo Dinfo)
    {
         Dinfo.Info = this;
    }

        private void detach_ToDo(DetailInfo Dinfo)
    {
          Dinfo.Info = null;
    }
}

如果我们使用普通列表它会非常有效,但我们无法使用列表创建映射

Json 输出

"result":{
"taskarray":[
{
"lon":79.0630932,
"status":1,
"expdate":1.39054962e12,
"date":"1390463355000",
"clientname":"pranit",
"id":1336,
"csoId":"we1141",
"title":"New Mobile",
"customercontact":"9595588794",
"address":"Dharampeth , Nagpur",
"priority":0,
"description":"none",
"comment":"none",
"note":"none",
"lat":21.1389989
},
{
"lon":79.0743915,
"status":1,
"expdate":1.39046334e12,
"date":"1390463446000",
"clientname":"bb",
"id":1337,
"csoId":"asdf",
"title":"xyz",
"customercontact":"9595588794",
"address":"Sadar, Nagpur",
"priority":2,
"description":"none",
"comment":"none",
"note":"none",
"lat":21.1632626
}
]
},
"status":"success",
"loccheckincom":"",
"code":"200"
}

【问题讨论】:

  • 你能告诉我们可变数据是什么样的吗?
  • 你能给我们看看json吗?
  • @guiomie,我输入了json格式

标签: c# linq serialization windows-phone-8


【解决方案1】:

您没有传递一个数组进行反序列化(正如例外所说)。您正在传递一个包含数组的对象,但不是实际的数组。您需要传递 json 对象的 taskarray 属性。

另外,您的 Info 类与您从服务器接收的 json 不匹配。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-07-21
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多