Anonymous
Вставьте текст JSON в SQL с помощью C#
Сообщение
Anonymous » 12 фев 2026, 23:02
У меня есть строка JSON, и я хочу вставить значения в базу данных SQL с помощью C#.
Код: Выделить всё
{
"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.\r\n\r\nSTEP 2: If user clicks it will redirect to the play store to any application which he/she haven’t downloaded before in their device.\r\n\r\nSTEP 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
}
}
....... и так далее
Я написал следующий код для вставки в набор данных, но он выдает ошибку.
Код: Выделить всё
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(JsonString);
DataTable dataTable = dataSet.Tables["request"];
Console.WriteLine(dataTable.Rows.Count);
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(row["Target"] + " - " + row["Format"]);
}
Ошибка получения:
информации: неожиданный токен JSON при чтении DataTable. Ожидал
StartArray, получил StartObject. Путь «запрос», строка 1, позиция 12.
Итак, помогите мне преобразовать Json в набор данных, а затем вставить в базу данных SQL или напрямую в базу данных SQL.
В настоящее время я использую newtonsoft.json.
Для моих целей также приветствуются другие простые варианты.
Подробнее здесь:
https://stackoverflow.com/questions/360 ... ng-c-sharp
1770926525
Anonymous
[b]У меня есть строка JSON, и я хочу вставить значения в базу данных SQL с помощью C#.[/b] [code]{ "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.\r\n\r\nSTEP 2: If user clicks it will redirect to the play store to any application which he/she haven’t downloaded before in their device.\r\n\r\nSTEP 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 } } [/code] ....... и так далее Я написал следующий код для вставки в набор данных, но он выдает ошибку. [code] 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(JsonString); DataTable dataTable = dataSet.Tables["request"]; Console.WriteLine(dataTable.Rows.Count); foreach (DataRow row in dataTable.Rows) { Console.WriteLine(row["Target"] + " - " + row["Format"]); } [/code] Ошибка получения: информации: неожиданный токен JSON при чтении DataTable. Ожидал StartArray, получил StartObject. Путь «запрос», строка 1, позиция 12. Итак, помогите мне преобразовать Json в набор данных, а затем вставить в базу данных SQL или напрямую в базу данных SQL. В настоящее время я использую newtonsoft.json. Для моих целей также приветствуются другие простые варианты. Подробнее здесь: [url]https://stackoverflow.com/questions/36061520/insert-json-text-into-sql-using-c-sharp[/url]