" System.ArgumentException:'不存在从对象类型Newtonsoft.Json.Linq.JValue到已知的托管提供程序本机类型的映射. [英] "System.ArgumentException: 'No mapping exists from object type Newtonsoft.Json.Linq.JValue to a known managed provider native type.'

查看:128
本文介绍了" System.ArgumentException:'不存在从对象类型Newtonsoft.Json.Linq.JValue到已知的托管提供程序本机类型的映射.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从响应中获取的JSON数组响应中插入数据.但是,我不断收到"System.ArgumentException:'从对象类型Newtonsoft.Json.Linq.JValue对象类型到已知的托管提供程序本机类型不存在映射.'"错误向我抛出.我拥有的代码和JSON数组如下:

I'm trying to insert data from a JSON Array response that I've gotten as a response. However, I keep getting a "System.ArgumentException: 'No mapping exists from object type Newtonsoft.Json.Linq.JValue to a known managed provider native type.'" error thrown at me. The code and JSON array that I have is as follows:

我尝试了谷歌搜索,向我的同伴寻求答案,但无济于事.我想知道,自序列化以来,是否需要更改JSON对象才能将其保存到数据库?同时,我得到的最常见的答案是添加.Text的扩展名,因为我的答复来自文本字段区域.

I've tried googling, asking my peers for the answers but to no avail. I was wondering, since being serialized, do JSON objects need to be changed before it can be saved to a database? At the same time, the most frequent answer that I have been given was to add in the extension of .Text as my responses come in a text field area.

JSON数组:

    "data": [
                {
                    "device": "deviceone",
                    "time": 2359,
                    "data": "0000th34"
}]

private void btnSave_Click(object sender, EventArgs e)
{
    string connectionString;
    connectionString = @"Data Source=LAPTOP-JOHN;Initial Catalog=DemoDb;    User ID=John;Password=1234";

    SqlConnection con = new SqlConnection(connectionString);
    //Opens the connection to the database
    con.Open();
    dynamic jsonObj = JsonConvert.DeserializeObject(txtResponse.Text);
    using (SqlCommand cmd = new SqlCommand("Insert into devicedata(device,time,data) VALUES (@device,@time,@data)", con))
    {
        cmd.Parameters.AddWithValue("@device", jsonObj.data[0].device);
        cmd.Parameters.AddWithValue("@time", jsonObj.data[0].time);
        cmd.Parameters.AddWithValue("@data", jsonObj.data[0].data);
        cmd.ExecuteNonQuery(); // Running the code will result in the error being thrown
    }
    con.Close()
}

如前所述,错误消息'没有从对象类型Newtonsoft.Json.Linq.JValue到已知托管提供程序本机类型的映射存在"引发错误.

As mentioned, the error gets thrown with the message "'No mapping exists from object type Newtonsoft.Json.Linq.JValue to a known managed provider native type."

推荐答案

尝试这样,即只需添加.ToString(),如下面的代码所示,因为rror来自发送对象作为参数值.它必须是字符串,整数,布尔值等.

try like this , i.e. just add .ToString()` as given in below code as rror comes from sending an object as a parameter value. It needs to be a string, int, bool, etc.

 using (SqlCommand cmd = new SqlCommand("Insert into devicedata(device,time,data) VALUES (@device,@time,@data)", con))
    {
        cmd.Parameters.AddWithValue("@device", jsonObj.data[0].device.ToString());
        cmd.Parameters.AddWithValue("@time", jsonObj.data[0].time.ToString());
        cmd.Parameters.AddWithValue("@data", jsonObj.data[0].data.ToString());
        cmd.ExecuteNonQuery(); // Running the code will result in the error being thrown
    }

这篇关于" System.ArgumentException:'不存在从对象类型Newtonsoft.Json.Linq.JValue到已知的托管提供程序本机类型的映射.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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