如何将json转换为本地数据库类属性 [英] how to convert json to local database class property

查看:97
本文介绍了如何将json转换为本地数据库类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将json值转换为本地数据库类问题 如果我使用list<>代替EntitySet可以正常工作

i have a problem with convert json value to local database class problem it works fine if i am use list<> in place of EntitySet

这是我的课程属性

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

when i am converting the below class from the code

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]'类型,因为 类型需要JSON数组(例如[1,2,3])才能正确反序列化.到 修复此错误,或者将JSON更改为JSON数组(例如[1,2,3]) 或更改反序列化类型,使其成为普通的.NET类型(例如 不是像整数这样的原始类型,不是像数组这样的集合类型 或列表),可以从JSON对象反序列化. 也可以将JsonObjectAttribute添加到类型中以强制将其 从JSON对象反序列化.路径"result.taskarray",第3行, 位置17.

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Data.Linq.EntitySet`1[ViewModel.Result]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'result.taskarray', line 3, position 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;
    }
}

如果我们使用标准列表"<>,它将非常有效,但是我们无法使用列表创建映射

If We uses the Normal List<> it works very Efficently but we can't create mapping with the list

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对象的taskarray属性.

You are not passing an array for deserialization (as the exception is saying). You are passing an object with an array in it, but not an actual array. You need to pass the taskarray property of your json object.

此外,您的Info类与您从服务器接收到的json不匹配.

Also, your Info class doesn't match with the json you are receiving from the server.

这篇关于如何将json转换为本地数据库类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆