2017-05-04 992 views
1

编辑 - 5/8的Json样本:SSIS脚本组件JSON DataContractJsonSerializer

{ 
"data": [ 
    { 
"evidenceId": "9999A999-9D99-4411-8819-DD55D9DDD55D", 
"status": "Active", 
"title": "Video 2017-04-30 2354", 
"idExternal": null, 
"description": null, 
"dateUploaded": "2017-05-01T01:33:10.522-06:00", 
"dateModified": "2017-05-01T01:33:10.970-06:00", 
"dateRecordStart": "2017-04-30T23:54:38.000-06:00", 
"dateRecordEnd": "2017-04-30T23:59:10.000-06:00", 
"dateDeleted": "2017-08-28T23:54:38.000-06:00", 
"evidenceType": "Video", 
"flag": "N", 
"contentType": "mp4", 
"sizeMb": 40.36958312988281, 
"durationSeconds": "272.76", 
"ownerFirstName": "D B", 
"ownerLastName": "ITE", 
"ownerBadgeId": "9999", 
"ownerRole": "Officer/PSA's", 
"ownerGroups": [], 
"updatedByFirstName": "", 
"updatedByLastName": "", 
"updatedByBadgeId": "", 
"updatedByRole": "", 
"deletedByFirstName": "", 
"deletedByLastName": "", 
"deletedByBadgeId": "", 
"deletedByRole": "", 
"uploadedByFirstName": "D B", 
"uploadedByLastName": "ITE", 
"uploadedByBadgeId": "9999", 
"uploadedByRole": "Officer/PSA's", 
"gps": { 
"latitude": null, 
"longitude": null 
}, 
"deviceId": "8888A888-8D88-4411-8819-DD55D9DDD66E", 
"notes": [], 
"categories": [ 
    "120 Day Deletion" 
], 
"tags": [], 
"cases": [], 
"viewCount": 0, 
"lastViewedOn": null, 
"isReassigned": false, 
"authenticatedShareCount": 0, 
"deletionType": "", 
"checksum": "{sha2}33aa3724c8b3bb647a27fb3a895867c8214508b0cdba6e66882095f22b29f23d", 
"downloadCount": 0 
}, 
    { 
"evidenceId": "7777A777-9E99-4411-8819-EE55E9EEE55E", 
"status": "Active", 
"title": "Video 2017-04-30 2354", 
"idExternal": null, 
"description": null, 
"dateUploaded": "2017-05-02T01:02:08.180-06:00", 
"dateModified": "2017-05-02T01:02:08.530-06:00", 
"dateRecordStart": "2017-04-30T23:54:04.000-06:00", 
"dateRecordEnd": "2017-05-01T00:01:53.000-06:00", 
"dateDeleted": "2017-08-28T23:54:04.000-06:00", 
"evidenceType": "Video", 
"flag": "N", 
"contentType": "mp4", 
"sizeMb": 68.5533447265625, 
"durationSeconds": "469.03", 
"ownerFirstName": "C S", 
"ownerLastName": "NARRO", 
"ownerBadgeId": "5555", 
"ownerRole": "Officer/PSA's", 
"ownerGroups": [], 
"updatedByFirstName": "", 
"updatedByLastName": "", 
"updatedByBadgeId": "", 
"updatedByRole": "", 
"deletedByFirstName": "", 
"deletedByLastName": "", 
"deletedByBadgeId": "", 
"deletedByRole": "", 
"uploadedByFirstName": "C S", 
"uploadedByLastName": "NARRO", 
"uploadedByBadgeId": "5555", 
"uploadedByRole": "Officer/PSA's", 
"gps": { 
"latitude": null, 
"longitude": null 
}, 
"deviceId": "2F87080C-8AB2-4BB5-A3F4-E8E51D648B79", 
"notes": [], 
"categories": [ 
    "120 Day Deletion" 
], 
"tags": [], 
"cases": [], 
"viewCount": 0, 
"lastViewedOn": null, 
"isReassigned": false, 
"authenticatedShareCount": 0, 
"deletionType": "", 
"checksum": "{sha2}47256fe19712a852198a5ac45aef949f4b1ad1011dddc16b6674add514a2d614", 
"downloadCount": 0 
} 
], 
} 

编辑 - 5/5附加信息:

这是我更新的我的实际项目的代码。我现在可以看到,如果我将参数限制为单个结果,我正在获取数据。但是当我增加结果时,我无法让foreach正常工作。我必须硬编码数组值。我感谢任何帮助,让这个工作!

#region Class 



[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 

public class ScriptMain : UserComponent 

{ 


    #region Methods 



    /// <summary>Outputs records to the output buffer</summary> 

    public override void CreateNewOutputRows() 

    { 


     //Set Webservice URL 

     string wUrl = "https://api.evidence.com/api/v1/agencies/{partnerId}/reports/data?reportType=EvidenceCreated&fromDate=2017-04-01&toDate=2017-04-30&pageSize=5&pageOffset=0"; 



     try 

     { 



      //Call getWebServiceResult to return our Article attributes 

      RootObject outPutResponse = GetWebServiceResult(wUrl); 


      //If we get data back 

      if (outPutResponse != null) 

      { 

       foreach (Datum ar in outPutResponse.data) 

       // { 

        //Output main attributes of Article 

        EvidenceBuffer.AddRow(); 

        EvidenceBuffer.evidenceId = outPutResponse.data[0].evidenceId; 

        EvidenceBuffer.status = outPutResponse.data[0].status; 

        EvidenceBuffer.title = outPutResponse.data[0].title; 

        EvidenceBuffer.idExternal = outPutResponse.data[0].idExternal; 

        EvidenceBuffer.description = outPutResponse.data[0].description; 

        EvidenceBuffer.dateUploaded = outPutResponse.data[0].dateUploaded; 

        EvidenceBuffer.dateModified = outPutResponse.data[0].dateModified; 

        EvidenceBuffer.dateRecordStart = outPutResponse.data[0].dateRecordStart; 

        EvidenceBuffer.dateRecordEnd = outPutResponse.data[0].dateRecordEnd; 

        EvidenceBuffer.dateDeleted = outPutResponse.data[0].dateDeleted; 

        EvidenceBuffer.evidenceType = outPutResponse.data[0].evidenceType; 

        EvidenceBuffer.flag = outPutResponse.data[0].flag; 

        EvidenceBuffer.contentType = outPutResponse.data[0].contentType; 

        EvidenceBuffer.sizeMb = outPutResponse.data[0].sizeMb; 

        EvidenceBuffer.durationSeconds = outPutResponse.data[0].durationSeconds; 

        EvidenceBuffer.ownerFirstName = outPutResponse.data[0].ownerFirstName; 

        EvidenceBuffer.ownerLastName = outPutResponse.data[0].ownerLastName; 

        EvidenceBuffer.ownerBadgeId = outPutResponse.data[0].ownerBadgeId; 

        EvidenceBuffer.ownerRole = outPutResponse.data[0].ownerRole; 

        EvidenceBuffer.ownerGroups = outPutResponse.data[0].ownerGroups; 

        EvidenceBuffer.updatedByFirstName = outPutResponse.data[0].updatedByFirstName; 

        EvidenceBuffer.updatedByLastName = outPutResponse.data[0].updatedByLastName; 

        EvidenceBuffer.updatedByBadgeId = outPutResponse.data[0].updatedByBadgeId; 

        EvidenceBuffer.updatedByRole = outPutResponse.data[0].updatedByRole; 

        EvidenceBuffer.deletedByFirstName = outPutResponse.data[0].deletedByFirstName; 

        EvidenceBuffer.deletedByLastName = outPutResponse.data[0].deletedByLastName; 

        EvidenceBuffer.deletedByBadgeId = outPutResponse.data[0].deletedByBadgeId; 

        EvidenceBuffer.deletedByRole = outPutResponse.data[0].deletedByRole; 

        EvidenceBuffer.uploadedByFirstName = outPutResponse.data[0].uploadedByFirstName; 

        EvidenceBuffer.uploadedByLastName = outPutResponse.data[0].uploadedByLastName; 

        EvidenceBuffer.uploadedByBadgeId = outPutResponse.data[0].uploadedByBadgeId; 

        EvidenceBuffer.uploadedByRole = outPutResponse.data[0].uploadedByRole; 

        EvidenceBuffer.gpslat = null; 

        EvidenceBuffer.gpslong = null; 

        EvidenceBuffer.deviceId = outPutResponse.data[0].deviceId; 

        EvidenceBuffer.notes = outPutResponse.data[0].notes[0]; 

        EvidenceBuffer.categories = outPutResponse.data[0].categories[0]; 


        EvidenceBuffer.tags = outPutResponse.data[0].tags[0]; 

        EvidenceBuffer.cases = outPutResponse.data[0].cases[0]; 

        EvidenceBuffer.viewCount = outPutResponse.data[0].viewCount; 

        EvidenceBuffer.lastViewedOn = outPutResponse.data[0].lastViewedOn; 

        EvidenceBuffer.isReassigned = outPutResponse.data[0].isReassigned; 

        EvidenceBuffer.authenicatedShareCount = outPutResponse.data[0].authenticatedShareCount; 

        EvidenceBuffer.deletionType = outPutResponse.data[0].deletionType; 

        EvidenceBuffer.checksum = outPutResponse.data[0].checksum; 

        EvidenceBuffer.downloadCount = outPutResponse.data[0].downloadCount; 


       //} 

      } 

     } 

     catch (Exception e) 

     { 

      FailComponent(e.ToString()); 

     } 


    } 


    /// <summary> 

    /// Method to return our list articles 

    /// </summary> 

    /// <param name="wUrl">The web service URL to call</param> 

    /// <returns>An object that contains a list of Articles</returns> 



    private RootObject GetWebServiceResult(string wUrl) 

    { 



     HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl); 

     httpWReq.Headers.Add("Authorization", "Bearer SecretCode"); 

     httpWReq.Method = "GET"; 

     httpWReq.ContentType = "application/json"; 

     HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse(); 


     RootObject jsonResponse = null; 


     try 

     { 

      //Get the stream of JSON 

      Stream responseStream = httpWResp.GetResponseStream(); 




      //Deserialize the JSON stream 

      using (StreamReader reader = new StreamReader(responseStream)) 

      { 



       //Deserialize our JSON 

       DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(RootObject)); 

       jsonResponse = (RootObject)sr.ReadObject(responseStream); 

      } 




     } 

     //Output JSON parsing error 

     catch (Exception e) 

     { 

      FailComponent(e.ToString()); 

     } 

     return jsonResponse; 


    } 


    /// <summary> 

    /// Outputs error message 

    /// </summary> 

    /// <param name="errorMsg">Full error text</param> 

    private void FailComponent(string errorMsg) 

    { 

     bool fail = false; 

     IDTSComponentMetaData100 compMetadata = this.ComponentMetaData; 

     compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail); 


    } 

    #endregion 

} 

#endregion 


#region JSON Classes 

//Class to hold attributes of the Article 

[DataContract] 

public class Gps 
{ 
    [DataMember(Name = "latitude")] 
    public object latitude { get; set; } 

    [DataMember(Name = "longitude")] 
    public object longitude { get; set; } 
} 

public class Datum 
{ 
    [DataMember(Name = "evidenceId")] 
    public string evidenceId { get; set; } 

    [DataMember(Name = "status")] 
    public string status { get; set; } 

    [DataMember(Name = "title")] 
    public string title { get; set; } 

    [DataMember(Name = "idExternal")] 
    public string idExternal { get; set; } 

    [DataMember(Name = "description")] 
    public string description { get; set; } 

    [DataMember(Name = "dateUploaded")] 
    public string dateUploaded { get; set; } 

    [DataMember(Name = "dateModified")] 
    public string dateModified { get; set; } 

    [DataMember(Name = "dateRecordStart")] 
    public string dateRecordStart { get; set; } 

    [DataMember(Name = "dateRecordEnd")] 
    public string dateRecordEnd { get; set; } 

    [DataMember(Name = "dateDeleted")] 
    public string dateDeleted { get; set; } 

    [DataMember(Name = "evidenceType")] 
    public string evidenceType { get; set; } 

    [DataMember(Name = "flag")] 
    public string flag { get; set; } 

    [DataMember(Name = "contentType")] 
    public string contentType { get; set; } 

    [DataMember(Name = "sizeMb")] 
    public float sizeMb { get; set; } 

    [DataMember(Name = "durationSeconds")] 
    public float durationSeconds { get; set; } 

    [DataMember(Name = "ownerFirstName")] 
    public string ownerFirstName { get; set; } 

    [DataMember(Name = "ownerLastName")] 
    public string ownerLastName { get; set; } 

    [DataMember(Name = "ownerBadgeId")] 
    public string ownerBadgeId { get; set; } 

    [DataMember(Name = "ownerRole")] 
    public string ownerRole { get; set; } 

    [DataMember(Name = "ownerGroups")] 
    public string ownerGroups { get; set; } 

    [DataMember(Name = "updatedByFirstName")] 
    public string updatedByFirstName { get; set; } 

    [DataMember(Name = "updatedByLastName")] 
    public string updatedByLastName { get; set; } 

    [DataMember(Name = "updatedByBadgeId")] 
    public string updatedByBadgeId { get; set; } 

    [DataMember(Name = "updatedByRole")] 
    public string updatedByRole { get; set; } 

    [DataMember(Name = "deletedByFirstName")] 
    public string deletedByFirstName { get; set; } 

    [DataMember(Name = "deletedByLastName")] 
    public string deletedByLastName { get; set; } 

    [DataMember(Name = "deletedByBadgeId")] 
    public string deletedByBadgeId { get; set; } 

    [DataMember(Name = "deletedByRole")] 
    public string deletedByRole { get; set; } 

    [DataMember(Name = "uploadedByFirstName")] 
    public string uploadedByFirstName { get; set; } 

    [DataMember(Name = "uploadedByLastName")] 
    public string uploadedByLastName { get; set; } 

    [DataMember(Name = "uploadedByBadgeId")] 
    public string uploadedByBadgeId { get; set; } 

    [DataMember(Name = "uploadedByRole")] 
    public string uploadedByRole { get; set; } 

    [DataMember(Name = "gps")] 
    public Gps gps { get; set; } 

    [DataMember(Name = "deviceId")] 
    public string deviceId { get; set; } 

    [DataMember(Name = "notes")] 
    public List<string> notes { get; set; } 

    [DataMember(Name = "categories")] 
    public List<string> categories { get; set; } 

    [DataMember(Name = "tags")] 
    public List<string> tags { get; set; } 

    [DataMember(Name = "cases")] 
    public List<string> cases { get; set; } 

    [DataMember(Name = "viewCount")] 
    public int viewCount { get; set; } 

    [DataMember(Name = "lastViewedOn")] 
    public string lastViewedOn { get; set; } 

    [DataMember(Name = "isReassigned")] 
    public bool isReassigned { get; set; } 

    [DataMember(Name = "authenticatedShareCount")] 
    public int authenticatedShareCount { get; set; } 

    [DataMember(Name = "deletionType")] 
    public string deletionType { get; set; } 

    [DataMember(Name = "checksum")] 
    public string checksum { get; set; } 

    [DataMember(Name = "downloadCount")] 
    public int downloadCount { get; set; } 
} 


//Root object that contains a List of data 

[DataContract] 


public class RootObject 
{ 
    [DataMember(Name = "data")] 
    public List<Datum> data { get; set; } 
} 

#endregion 

原帖:

我使用这个样本,试图得到一个SSIS包从一个API来消费。我无法让我的JSON正常工作(并且示例中使用的API已移动,因此我无法按原样使其工作),因此我决定尝试一些简单的方法。

https://dennysjymbo.blogspot.com/2014/03/utilizing-net-40-datacontractjsonserial.html?showComment=1493916532059#c208608008820233205

我试图创建一个简单的SSIS包从https://swapi.co/api/people/1

这里消费这个信息是我的脚本:

#region Namespaces 

using System; 

using System.Data; 

using Microsoft.SqlServer.Dts.Pipeline.Wrapper; 

using Microsoft.SqlServer.Dts.Runtime.Wrapper; 

using System.Net; 

using Microsoft.SqlServer.Dts.Runtime; 

using System.IO; 

using System.Runtime.Serialization.Json; 

using System.Runtime.Serialization; 

using System.Collections.Generic; 

using System.Text; 

#endregion 


#region Class 



[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 

public class ScriptMain : UserComponent 

{ 


    #region Methods 



    /// <summary>Outputs records to the output buffer</summary> 

    public override void CreateNewOutputRows() 

    { 


     //Set Webservice URL 

     //string wUrl = "https://api.evidence.com/api/v1/agencies/3DCB15D3-3770-4DC4-8D22-4FB1FA8619A5/reports/data?reportType=EvidenceCreated&fromDate=2017-04-01&toDate=2017-04-30&pageSize=5&pageOffset=0"; 
     string wUrl = "http://swapi.co/api/people/1"; 


     try 

     { 



      //Call getWebServiceResult to return our Article attributes 

      RootObject outPutResponse = GetWebServiceResult(wUrl); 


      //If we get data back 

      if (outPutResponse != null) 

      { 

       foreach (People ar in outPutResponse.people) 

       { 

        //Output main attributes of Article 

        PeopleBuffer.AddRow(); 

        PeopleBuffer.name = ar.name; 

        PeopleBuffer.height = ar.height; 

        PeopleBuffer.mass = ar.mass; 

        PeopleBuffer.haircolor = ar.hair_color; 

        PeopleBuffer.skincolor = ar.skin_color; 

        PeopleBuffer.eyecolor = ar.eye_color; 

        PeopleBuffer.birthyear = ar.birth_year; 

        PeopleBuffer.gender = ar.gender; 

        PeopleBuffer.homeworld = ar.homeworld; 

        PeopleBuffer.films = ar.films[0]; 

        PeopleBuffer.species = ar.species[0]; 

        PeopleBuffer.vehicles = ar.vehicles[0]; 

        PeopleBuffer.starships = ar.starships[0]; 

        PeopleBuffer.created = ar.created; 

        PeopleBuffer.edited = ar.edited; 

        PeopleBuffer.url = ar.url; 

        } 

      } 

     } 

     catch (Exception e) 

     { 

      FailComponent(e.ToString()); 

     } 


    } 


    /// <summary> 

    /// Method to return our list articles 

    /// </summary> 

    /// <param name="wUrl">The web service URL to call</param> 

    /// <returns>An object that contains a list of Articles</returns> 



    private RootObject GetWebServiceResult(string wUrl) 

    { 



     HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl); 

     //httpWReq.Headers.Add("Authorization", "Bearer HxS35IIN36b9EW64L+GG3xYhoz66bNaD8hsckfQGPdk="); 

     httpWReq.Method = "GET"; 

     httpWReq.ContentType = "application/json"; 

     HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse(); 


     RootObject jsonResponse = null; 


     try 

     { 

      //Get the stream of JSON 

      Stream responseStream = httpWResp.GetResponseStream(); 




      //Deserialize the JSON stream 

      using (StreamReader reader = new StreamReader(responseStream)) 

      { 



       //Deserialize our JSON 

       DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(RootObject)); 

       jsonResponse = (RootObject)sr.ReadObject(responseStream); 

      } 




     } 

     //Output JSON parsing error 

     catch (Exception e) 

     { 

      FailComponent(e.ToString()); 

     } 

     return jsonResponse; 


    } 


    /// <summary> 

    /// Outputs error message 

    /// </summary> 

    /// <param name="errorMsg">Full error text</param> 

    private void FailComponent(string errorMsg) 

    { 

     bool fail = false; 

     IDTSComponentMetaData100 compMetadata = this.ComponentMetaData; 

     compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail); 


    } 

    #endregion 

} 

#endregion 


#region JSON Classes 

//Class to hold attributes of the Article 

[DataContract] 


public class People 
{ 
    [DataMember(Name = "name")] 
    public string name { get; set; } 

    [DataMember(Name = "height")] 
    public string height { get; set; } 

    [DataMember(Name = "mass")] 
    public string mass { get; set; } 

    [DataMember(Name = "hair_color")] 
    public string hair_color { get; set; } 

    [DataMember(Name = "skin_color")] 
    public string skin_color { get; set; } 

    [DataMember(Name = "eye_color")] 
    public string eye_color { get; set; } 

    [DataMember(Name = "birth_year")] 
    public string birth_year { get; set; } 

    [DataMember(Name = "gender")] 
    public string gender { get; set; } 

    [DataMember(Name = "homeworld")] 
    public string homeworld { get; set; } 

    [DataMember(Name = "films")] 
    public List<string> films { get; set; } 

    [DataMember(Name = "species")] 
    public List<string> species { get; set; } 

    [DataMember(Name = "vehicles")] 
    public List<string> vehicles { get; set; } 

    [DataMember(Name = "starships")] 
    public List<string> starships { get; set; } 

    [DataMember(Name = "created")] 
    public DateTime created { get; set; } 

    [DataMember(Name = "edited")] 
    public DateTime edited { get; set; } 

    [DataMember(Name = "url")] 
    public string url { get; set; } 
} 

//Root object that contains a List of Articles 

[DataContract] 

public class RootObject 
{ 
    [DataMember(Name = "people")] 

    public List<People> people { get; set; } 
} 


#endregion 

这是无法正常工作的一行:

jsonResponse = (RootObject)sr.ReadObject(responseStream); 

在图像中,您可以看到它添加了一个人物对象,但没有数据。

enter image description here

我希望搞清楚什么,我需要为了得到在响应对象中的数据修改任何帮助。

谢谢!

编辑: 的SSIS包提供了实际的错误信息是:

Error: 0x1 at Data Flow Task, Error Getting Data From Webservice!: System.NullReferenceException: Object reference not set to an instance of an object. 
    at ScriptMain.CreateNewOutputRows() in c:\Users\E37026\AppData\Local\Temp\Vsta\747a4fee6a1f4610a91610400e06a1ac\main.cs:line 81 
+0

任何方式,你可以提供一个去识别json样本? – jymbo

+0

@jymbo - 这里是JSON示例!谢谢! – Leslie

+0

我得到它的工作!因为RootObject是一个列表而不是集合,所以我需要将foreach更改为具有索引值的for! – Leslie

回答

1

夫妇在这里的事情。首先你的网址应该是http://swapi.co/api/people/1/?format=json。其次,你没有得到一批人,所以你需要改变这个单一的对象。第三,序列化程序不喜欢日期时间值,我将它们更改为字符串,并且工作正常。您可以在暂存区域或派生列转换中进行更改。这里是代码:

#region Namespaces 
using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Pipeline.Wrapper; 
using Microsoft.SqlServer.Dts.Runtime.Wrapper; 
using System.Net; 
using Microsoft.SqlServer.Dts.Runtime; 
using System.IO; 
using System.Runtime.Serialization.Json; 
using System.Runtime.Serialization; 
using System.Collections.Generic; 
using System.Text; 
#endregion 

#region Class 
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 
public class ScriptMain : UserComponent 
{ 

#region Methods 



/// <summary>Outputs records to the output buffer</summary> 

public override void CreateNewOutputRows() 
{ 


    //Set Webservice URL 

    //string wUrl = "https://api.evidence.com/api/v1/agencies/3DCB15D3-3770-4DC4-8D22-4FB1FA8619A5/reports/data?reportType=EvidenceCreated&fromDate=2017-04-01&toDate=2017-04-30&pageSize=5&pageOffset=0"; 
    string wUrl = "http://swapi.co/api/people/1/?format=json"; 


    try 
    { 



     //Call getWebServiceResult to return our Article attributes 

     People outPutResponse = GetWebServiceResult(wUrl); 


     //If we get data back 

     if (outPutResponse != null) 
     { 



       //Output main attributes of Article 

       PeopleBuffer.AddRow(); 

       PeopleBuffer.name = outPutResponse.name; 

       PeopleBuffer.height = outPutResponse.height; 

       PeopleBuffer.mass = outPutResponse.mass; 

       PeopleBuffer.haircolor = outPutResponse.hair_color; 

       PeopleBuffer.skincolor = outPutResponse.skin_color; 

       PeopleBuffer.eyecolor = outPutResponse.eye_color; 

       PeopleBuffer.birthyear = outPutResponse.birth_year; 

       PeopleBuffer.gender = outPutResponse.gender; 

       PeopleBuffer.homeworld = outPutResponse.homeworld; 

       PeopleBuffer.films = outPutResponse.films[0]; 

       PeopleBuffer.species = outPutResponse.species[0]; 

       PeopleBuffer.vehicles = outPutResponse.vehicles[0]; 

       PeopleBuffer.starships = outPutResponse.starships[0]; 

       PeopleBuffer.created = outPutResponse.created; 

       PeopleBuffer.edited = outPutResponse.edited; 

       PeopleBuffer.url = outPutResponse.url; 

     } 

    } 

    catch (Exception e) 
    { 

     FailComponent(e.ToString()); 

    } 


} 


/// <summary> 

/// Method to return our list articles 

/// </summary> 

/// <param name="wUrl">The web service URL to call</param> 

/// <returns>An object that contains a list of Articles</returns> 



private People GetWebServiceResult(string wUrl) 
{ 



    HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl); 

    //httpWReq.Headers.Add("Authorization", "Bearer HxS35IIN36b9EW64L+GG3xYhoz66bNaD8hsckfQGPdk="); 

    httpWReq.Method = "GET"; 

    httpWReq.ContentType = "application/json"; 

    HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse(); 


    People jsonResponse = null; 


    try 
    { 

     //Get the stream of JSON 

     Stream responseStream = httpWResp.GetResponseStream(); 




     //Deserialize the JSON stream 

     using (StreamReader reader = new StreamReader(responseStream)) 
     { 



      //Deserialize our JSON 

      DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(People)); 

      jsonResponse = (People)sr.ReadObject(responseStream); 

     } 




    } 

    //Output JSON parsing error 

    catch (Exception e) 
    { 

     FailComponent(e.ToString()); 

    } 

    return jsonResponse; 


} 


/// <summary> 

/// Outputs error message 

/// </summary> 

/// <param name="errorMsg">Full error text</param> 

private void FailComponent(string errorMsg) 
{ 

    bool fail = false; 

    IDTSComponentMetaData100 compMetadata = this.ComponentMetaData; 

    compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail); 


    } 

#endregion 

} 

#endregion 


#region JSON Classes 

//Class to hold attributes of the Article 

[DataContract] 
public class People 
{ 
[DataMember(Name = "name")] 
public string name { get; set; } 

[DataMember(Name = "height")] 
public string height { get; set; } 

[DataMember(Name = "mass")] 
public string mass { get; set; } 

[DataMember(Name = "hair_color")] 
public string hair_color { get; set; } 

[DataMember(Name = "skin_color")] 
public string skin_color { get; set; } 

[DataMember(Name = "eye_color")] 
public string eye_color { get; set; } 

[DataMember(Name = "birth_year")] 
public string birth_year { get; set; } 

[DataMember(Name = "gender")] 
public string gender { get; set; } 

[DataMember(Name = "homeworld")] 
public string homeworld { get; set; } 

[DataMember(Name = "films")] 
public List<string> films { get; set; } 

[DataMember(Name = "species")] 
public List<string> species { get; set; } 

[DataMember(Name = "vehicles")] 
public List<string> vehicles { get; set; } 

[DataMember(Name = "starships")] 
public List<string> starships { get; set; } 

[DataMember(Name = "created")] 
public string created { get; set; } 

[DataMember(Name = "edited")] 
public string edited { get; set; } 

[DataMember(Name = "url")] 
public string url { get; set; } 
} 



#endregion 
+0

非常感谢你 - 这工作!然而,这只是我的实践例子,试图让一些工作!我的最终产品将返回多个记录。如果我有任何其他问题,我会尝试进行所需的更改以便正常工作并回传。 – Leslie