【问题标题】:JSON to SQL Table [duplicate]JSON 到 SQL 表 [重复]
【发布时间】:2016-07-05 02:13:21
【问题描述】:

我有以下从 API 获取并保存在 C# 中的对象中的 JSON 数据,并希望它插入 SQL 数据库中。

{
"6389": {
    "Offer": {
      "id": "6389",
      "name": "Swim",
      "description": "Incent",
      "require_approval": "1",
     }
  },
  "6141": {
    "Offer": {
      "id": "6141",
      "name": "Express",
      "description": "Incent",
      "require_approval": "1",
      }
  },
  "5677": {
    "Offer": {
      "id": "5677",
      "name": "Alive",
      "description": "",
      "require_approval": "1",
     }
  },
  "6669": {
    "Offer": {
      "id": "6669",
      "name": "All",
      "description": "Incent\r\",
      "require_approval": "1",
     }
  },
  "6767": {
    "Offer": {
      "id": "6767",
      "name": "App",
      "description": "",
      "require_approval": "1",
    }
  }

}

我想将上面的 JSON 保存为以下格式的 SQL 数据库表。

ID      name       description  require_approval
6389    Swim       Incent             1
6141    Express    Incent             1
5677    Alive                         1
6669    All        Incent/r/          1
6767    App                           1

提前致谢。

【问题讨论】:

  • 假设数据中的任何差异只是为了方便,您在将数据插入数据库时​​遇到什么问题?
  • JSON 和 table 之间的数据不匹配是错误的。你知道我应该怎么做才能在 SQL 表中插入以下 JSON 吗?
  • 我是 C# 编码的新手,我不知道如何使用它们。如果您知道,您能否提供代码或任何参考链接?谢谢。
  • @Sindhu:你两次提出同样的问题吗? stackoverflow.com/questions/36061520/…

标签: c# sql json json.net


【解决方案1】:

你需要在你的代码中实现这样的东西。

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
  class Program
  {
      static void Main(string[] args)
      {
        HttpClient client = new HttpClient();

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync("https://ebanking.bankofmaldives.com.mv/xe/").Result;


        if (response.IsSuccessStatusCode)
        {

            string JSON = response.Content.ReadAsStringAsync().Result;
            //Deserialize to strongly typed class i.e., RootObject
            RootObject obj = JsonConvert.DeserializeObject<RootObject>(JSON);
            //Suppose your connection string is with the name XYZEntities in web.config, and your database contains the table with proper structure required. Supposing table name as XYZ, then what you need to do is:
            //loop through the list and insert into database
            using(var db = new XYZEntities){
            foreach (CurrencyList currencyItem in obj.CurrencyList)
            {
                  db.XYZ.Add(new XYZ(){
                  key = currencyItem.Key,
                  code = currencyItem.Value.Code, 
                  description = currencyItem.Value.Description,
                  buy = currencyItem.Value.Buy,
                  sell = currencyItem.Value.Sell
                  });                  
            }
            try{
                 db.SaveChanges();
               }catch(Exception e){
                //do something to handle exception here.
               }
          }

        }
    }

    **Note: Below classes are generated by utilising http://json2csharp.com/**
    **ofcourse these can be hand coded by looking at the Json response from the service**

    public class Value
    {
        public string Code { get; set; }
        public string Description { get; set; }
        public double Buy { get; set; }
        public double Sell { get; set; }
    }

    public class CurrencyList
    {
        public string Key { get; set; }
        public Value Value { get; set; }
    }

    public class RootObject
    {
        public List<CurrencyList> CurrencyList { get; set; }
    }

  }
} 

您需要创建一个表并将这些对象中的数据插入到该表中。请参考How to save json to Sql Database

【讨论】:

  • 不幸的是,这根本没有显示如何将数据放入数据库表中(这似乎是提问者的问题所在)。
  • 除非 Sindhu 已经在项目中设置了实体框架并添加了他们的表,否则这并没有多大意义(另外,我认为这个问题太宽泛,根据他们的知识无法很好地回答) .不错的尝试。
  • 这就是为什么我没有提到插入数据库的细节。但是逻辑是一样的,不管他用的是哪个数据库。
  • 终于出来了:他们正在使用 SQL Table Adapter 类。因此,您使用实体框架的示例将无济于事。
猜你喜欢
  • 2017-08-11
  • 2021-09-02
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
相关资源
最近更新 更多