簡體   English   中英

從 JSON 獲取數據或值

[英]Get data or values from JSON

我正在 SSIS 中創建一個解決方案以從 API/url 獲取數據,示例如下: URL is obviously is different in actual project

https://{你的服務器}}/api/v1/{你的賬戶}/internalqueries?queryguid=F6AC1BB3-DC03-4E64-BC46-30DEDE3BD27E&filterguid=AAED4F63-76CC-4E09-8C97-2C425D4E48EF&viewguid=DD4F7938-951F-4279Arow_6B35B =3

預期的 JSON 結果:

{
    "HREF": "https://{your_server}/api/v1/{your_account}/internalqueries?queryguid=%7B6938436D-B024-4B9E-9815-A41C1D7C7A0E%7D&filterguid=%7B2FAC2998-B0EC-45BF-9B98-1A09B1F8C343%7D&viewguid=%7B631587D5-EED1-49C7-9252-54E08A398CDE%7D&max_rows=3",
    "recordcount": 3,
    "previouspage": 0,
    "nextpage": 0,
    "records": [
        {
            "Creation Date": "1/9/2017 11:07:34 am",
            "Incident Number": "I181218_000003",
            "Category": "Get Help",
            "Title": "",
            "Recipient": "William",
            "Description": "",
            "Manager": "",
            "Status": "In Progress",
            "Time Status": "4",
            "Priority": "2",
            "SLA Target": "1/11/2017 11:07:34 am",
            "Cost": "70.00",
            "End Date": ""
        },
        
    ]
}

創建了一個名為 ResultAPI 的類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SC_b15eeeff0e3544d3a882578b3eb9bbba
{
    public class ResultAPI
    {
        public string HREF { get; set; }
        public int recordcount { get; set; }
        public int previouspage { get; set; }
        public string Recipient { get; set }
        public string records { get; set; }
    }
}

另一個名為 GenericResult 的類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SC_b15eeeff0e3544d3a882578b3eb9bbba
{
    public class GenericResponse

    {
        public ResultAPI[] ListData { get; set; }
    }
    public class ResultGen
    {
        public GenericResponse Result { get; set; }
    }
}

主要的:

public override void CreateNewOutputRows()
{

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("https://{your_server}}/api/v1/{your_account}/internalqueries?queryguid=F6AC1BB3-DC03-4E64-BC46-30DEDE3BD27E&filterguid=AAED4F63-76CC-4E09-8C97-2C425D4E48EF&viewguid=DD4F7938-951F-4279-953B-A6EB075FCB35&max_rows=3");
        client.DefaultRequestHeaders.Accept.Clear();    
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

    var cred = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "username", "Password")));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", cred);

    string APIUrl = ("https://{your_server}}/api/v1/{your_account}/internalqueries?queryguid=F6AC1BB3-DC03-4E64-BC46-30DEDE3BD27E&filterguid=AAED4F63-76CC-4E09-8C97-2C425D4E48EF&viewguid=DD4F7938-951F-4279-953B-A6EB075FCB35&max_rows=3");


    var response = client.GetAsync(APIUrl).Result;
        if (response.IsSuccessStatusCode)
        {
            var result = response.Content.ReadAsStringAsync().Result;
            var serializer = new JavaScriptSerializer();
            serializer.MaxJsonLength = Int32.MaxValue;
            var data = serializer.Deserialize<ResultAPI>(result);

        APIResultsBuffer.AddRow();
        APIResultsBuffer.previouspage = data.previouspage;
        APIResultsBuffer.recordcount = data.recordcount;
        APIResultsBuffer.HREF = data.HREF;
       
        // Getting error here Property of indexer cannot be assigned to---it is read only**
       APIResultsBuffer.records = data.records;
       // this is just for illustration if I want to get actual values of the Recipient field in the SQL table**
       // *APIResultsBuffer.Recipient= data.Recipient*
        
    }
    
    }
    
}

我將 SQL Server DB 作為 OLE 目標,我想弄清楚的是如何在 SQL Server 表中獲取實際記錄,我得到的只是 HREF、previouspage 和 recordcount 正確,其他一切都是空白,但應該是什么腳本組件中記錄的數據類型——輸入和輸出以及我需要進行哪些更改才能從上面的 JSON 中獲取實際的收件人數據。

任何幫助表示贊賞。

嘗試更改記錄的數據類型,但似乎沒有任何效果

更新:我更新了 CreatedNewOutput 部分:

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue; 
List<ResultAPI> suppliers = serializer.Deserialize<List<ResultAPI>>(APIUrl); 
foreach (ResultAPI resultAPI in suppliers) 
    APIResultsBuffer.HREF = resultAPI.Recipient;

我收到一個錯誤:無效的 JSON 原語:https。

講話

// Getting error here Property of indexer cannot be assigned to---it is read only**
APIResultsBuffer.records = data.records;

我猜您將 SSIS 中的記錄列定義為 DT_TEXT 或 DT_NTEXT,這就是您收到該錯誤的原因。 您不能直接為 SSIS 中的 blob 類型賦值。 相反,您需要調用AddBlobData但您只能傳入一個字節數組,因此您需要從How do I convert a String to a BlobColumn in SSIS

    Output0Buffer.AddRow();
    string aString = "X".PadRight(10000) + "X";
    // https://stackoverflow.com/questions/35703025/how-do-i-convert-a-string-to-a-blobcolumn-in-ssis
    byte[] bytes = new byte[aString.Length * sizeof(char)];
    System.Buffer.BlockCopy(aString.ToCharArray(), 0, bytes, 0, bytes.Length);
    Output0Buffer.RecordsText.AddBlobData(bytes);

暫無
暫無

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

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