Insert JSON text into SQL using C#(使用 C# 将 JSON 文本插入 SQL)
问题描述
我有以下 JSON 字符串,我想使用 C# 在 SQL 数据库中插入值.
{
"request": {
"Target": "Affiliate",
"Format": "jsonp",
"Service": "Offers",
"Version": "2",
"NetworkId": "dotcominfoway",
"Method": "findAll",
"api_key": "4bf7ba7b1904716179c9284cbd",
"callback": "angular.callbacks._2",
"_ga": "GA1.2.894200611.1458193988"
},
"response": {
"status": 1,
"httpStatus": 200,
"data": {
"2204": {
"Offer": {
"id": "2204",
"name": "App Of the Day Android IN Incent",
"description": "STEP 1 : You can place your own logo/creative in the offer wall as you like or you can place our creative.
STEP 2: If user clicks it will redirect to the play store to any application which he/she haven’t downloaded before in their device.
STEP 3 : User have to install the application and open it.",
"require_approval": "1",
"require_terms_and_conditions": 0,
"terms_and_conditions": null,
"preview_url": "http://appfly.mobi/red/02b4ef54-144b-11e5-a076-0cc47a44dbaa/?alg=2",
"currency": null,
"default_payout": "0.20000",
"status": "active",
"expiration_date": "2016-06-17 03:59:59",
"payout_type": "cpa_flat",
"percent_payout": "100.00",
"featured": null,
"conversion_cap": "0",
"monthly_conversion_cap": "0",
"payout_cap": "0.00",
"monthly_payout_cap": "0.00",
"allow_website_links": "0",
"allow_direct_links": "0",
"show_custom_variables": "0",
"show_mail_list": "0",
"dne_list_id": "0",
"email_instructions": "0",
"email_instructions_from": "",
"email_instructions_subject": "",
"has_goals_enabled": "0",
"default_goal_name": "",
"use_target_rules": "0",
"is_expired": "0",
"dne_download_url": null,
"dne_unsubscribe_url": null,
"dne_third_party_list": false
}
},
"3669": {
"Offer": {
"id": "3669",
"name": "Cash On IN Incent CPR",
"description": "Automatic OTP",
"require_approval": "1",
"require_terms_and_conditions": 0,
"terms_and_conditions": null,
"preview_url": "https://play.google.com/store/apps/details?id=com.softn",
"currency": "INR",
"default_payout": "12.00000",
"status": "active",
"expiration_date": "2016-09-29 03:59:59",
"payout_type": "cpa_flat",
"percent_payout": null,
"featured": null,
"conversion_cap": "1000",
"monthly_conversion_cap": "0",
"payout_cap": "0.00",
"monthly_payout_cap": "0.00",
"allow_website_links": "0",
"allow_direct_links": "0",
"show_custom_variables": "0",
"show_mail_list": "0",
"dne_list_id": "0",
"email_instructions": "0",
"email_instructions_from": "",
"email_instructions_subject": "",
"has_goals_enabled": "0",
"default_goal_name": "",
"use_target_rules": "0",
"is_expired": "0",
"dne_download_url": null,
"dne_unsubscribe_url": null,
"dne_third_party_list": false
}
}
.......等等
我已经编写了以下代码插入数据集,但它给出了错误.
I have written following code to insert into Dataset but it is giving error.
string url = "http:api.offers.com/Apiv3/json?NetworkId=inf&Target=Affiliate_Offer&Method=findAll&api_key=4bf7ba7b1904716179c9284cbd7db17018b8a5f";
string JsonString = new WebClient().DownloadString(url);
DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(JsonString);
DataTable dataTable = dataSet.Tables["request"];
Console.WriteLine(dataTable.Rows.Count);
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(row["Target"] + " - " + row["Format"]);
}
获取错误:
信息:读取 DataTable 时出现意外的 JSON 令牌.预期的StartArray,得到了 StartObject.路径请求",第 1 行,位置 12.
information: Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path 'request', line 1, position 12.
所以,请帮我将 Json 转换为 Dataset,然后插入 SQL 数据库或直接插入 SQL 数据库.我目前正在使用 newtonsoft.json.出于我的目的,也欢迎其他简单的选项.
So, please help me to convert the Json into Dataset and then insert into SQL database or direct to SQL database. I'm currently using newtonsoft.json. Other easy options are also welcome for my purpose.
推荐答案
反序列化器不知道如何处理类型 DataSet.
The deserializer does not know what to do with the type DataSet.
看看这个答案:https://stackoverflow.com/a/11982180/1235106
这篇关于使用 C# 将 JSON 文本插入 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 C# 将 JSON 文本插入 SQL
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
