簡體   English   中英

在SQL中反序列化包含多個子數組和批量復制的Json對象

[英]Deserialize Json Object containing multiple sub Array and Bulk Copy in SQL

我有以下包含多個子數組的JSON數據,任何建議如何將其反序列化並打印在我的頁面上。

  • 下一步

    :之后,我需要使用批量復制功能在SQL中插入此數據。

C#代碼

collect mydata= new JavaScriptSerializer().Deserialize<collect >(json);
    foreach (var item in mydata.results)
    {
    context.Response.Write(item.newPrice + item.pName);
    }

 public class collect 
    {
        public List<collection1> results { get; set; }
    }

    public class collection1
    {
        public List<data> collection1 { get; set; }
    }
    public class data
    {
        public string newPrice { get; set; }
        public string pName { get; set; }
    }

JSON數組:

{
  "name": "Test 1",
  "count": 3,
  "version": 2,
  "lastsuccess": "Thu Oct 09 2014 05:42:17 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "newPrice": "12787",
        "pName": "Sony Xperia M Dual Black"
      },
      {
        "newPrice": "24999",
        "pName": "LG Google Nexus 5 16 GB (Black)"
      }
    ]
  }
}

要回答有關如何對JSON進行反序列化的問題,這是一個解決方案...我不確定您所說的“在我的頁面上打印”是什么意思,因為您的問題並未將其放在任何上下文中...

我使用http://json2csharp.com在下面創建了poco類...

public class Collection1
{
    public string newPrice { get; set; }
    public string pName { get; set; }
}

public class Results
{
    public List<Collection1> collection1 { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public int count { get; set; }
    public int version { get; set; }
    public string lastsuccess { get; set; }
    public Results results { get; set; }
}

然后以下代碼將JSON反序列化為C#...

RootObject myData = new JavaScriptSerializer().Deserialize<RootObject>(json);

就批量插入而言,您現在可以隨心所欲地使用它了……那真的是另一個問題,所以請開始一個新的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM