作为SSIS(2005)包的一部分,我通过脚本组件(VB.NET)调用RESTful API。我正在收回记录。我遇到的问题是处理它返回的格式,所以我可以在数据流中输出它(拆分成单独的输出列)并写入SQL表。不知道从哪里开始 - 任何人都有任何想法?我无法控制API,所以我坚持使用这种格式。
以下是API文档中的架构:
{StatusMessage : <string>, Unit : <string> [ { VehicleID : <int>, RegistrationNumber : <string>, DistanceTravelled : <double>} ] }
以下是返回数据的示例(它以单行文本形式返回):
{"VehicleDistance":
[
{
"VehicleId":508767,
"RegistrationNumber":"BJ63 NYO",
"DistanceTravelled":0.09322560578584671
},
{
"VehicleId":508788,
"RegistrationNumber":"BJ63 NYL",
"DistanceTravelled":6.1591048240661621
},
{
"VehicleId":508977,
"RegistrationNumber":"PE12 LLC",
"DistanceTravelled":60.975761413574219
},
{
"VehicleId":510092,
"RegistrationNumber":"BJ64 FCY",
"DistanceTravelled":14.369173049926758
},
{
"VehicleId":510456,
"RegistrationNumber":"BJ63 NYY",
"DistanceTravelled":4.04599142074585
},
{
"VehicleId":513574,
"RegistrationNumber":"BL64 AEM",
"DistanceTravelled":302.150390625
}
],
"StatusMessage":null,
"Unit":"imperial",
"HttpStatus":200
}
答案 0 :(得分:0)
这是Javscript Object Notation AKA JSON,你需要对它进行反序列化。问题是使用SSIS对于流行(并且速度快)的第三方工具来说很棘手但是VB.Net实际上有一个内置类来序列化和反序列化JSON,称为JavaScriptSerializer
首先添加一个名为System.Web.Extensions的项目的引用,然后您可以使用JavaScriptSerializer来反序列化您的JSON。
我已将您的JSON放在一个文件中以便于处理,所以首先我必须...
Dim sJSON As String = ""
Using swReadFile As New System.IO.StreamReader("E:\JSON.txt")
sJSON = swReadFile.ReadToEnd()
End Using
其余的是相关位,所以首先添加2个Imports ......
Imports System.Collections.Generic
Imports System.Web.Script.Serialization
然后我们可以......
Dim lvSerializer As JavaScriptSerializer = New JavaScriptSerializer()
lvSerializer.MaxJsonLength = 2147483644
Dim dictParsedJSONPairs As Dictionary(Of String, Object) = lvSerializer.Deserialize(Of Dictionary(Of String, Object))(sJSON)
If dictParsedJSONPairs.ContainsKey("VehicleDistance") AndAlso _
TypeOf dictParsedJSONPairs("VehicleDistance") Is ArrayList Then
Dim ArrayEntries As ArrayList = DirectCast(dictParsedJSONPairs("VehicleDistance"), ArrayList)
For Each ArrayEntry As Object In ArrayEntries
Dim DictEntry As Dictionary(Of String, Object) = DirectCast(ArrayEntry, Dictionary(Of String, Object))
If DictEntry.ContainsKey("VehicleId") Then Console.WriteLine("VehichleId:" & DictEntry("VehicleId"))
Next
End If
If dictParsedJSONPairs.ContainsKey("Unit") Then
Console.WriteLine("Unit is " & dictParsedJSONPairs.Item("Unit"))
End If
显然你应该在开始认真使用之前研究JSON。该对象可以是嵌套的JSON(即Dictionary(Of String,Object)),一些排序,一个字符串或一个ArrayList
答案 1 :(得分:0)
可能有点晚了,但你可能想看一下Newtonsoft(website)的json.net。提供的组件包含.Net 2.0版本。
在SSIS脚本任务中使用它非常简单。我在SSIS2008中基于.Net 3.5来解析JSON字符串,如下所示。根据该文档,它也适用于.Net 2.0版本。
//I assume you had obtained JSON in string format
string JasonBuffer;
//define Json object
JObject jObject = JObject.Parse(JasonBuffer);
//In my example the Json object starts with result
JArray Results = (JArray)jObject["result"];
//loop the result
foreach (JObject Result in Results)
{
//for simple object
JObject joCustomer = (JObject)Result["Customer"];
//do something...
//for complex object continue drill down
foreach (JObject joSection in Result["Sections"])
{
foreach (JObject joDepartment in joSection["Departments"])
{
foreach (JObject joItem in joDepartment["Items"])
{
}
您可以在此处找到一些实际代码:link