簡體   English   中英

從Web請求序列化JSON數組

[英]Serialize JSON array from a web request

我正在從SSIS的腳本任務中發出一個非常簡單的JSON請求。 URL返回非常大,有2000萬個字符。

第一個URL返回行如下所示:

[{"brandId":"9","season":"SG","year":"2003","bomNumber":"00011111","costId":"00001","bomCCNumber":"0000008","firstCost":1.01,"landedFactor":1.234,"elc":9.876,"market":"MA","channel":"CH","destinationCountry":"DC"},...

我已經通過解析器運行了URL返回,並驗證了它的格式正確。

這是我的代碼。

[DataContract]
public class CostingNegotiated
{

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

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

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

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

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

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

    [DataMember(Name = "firstCost")]
    public string firstCost { get; set; }
    [DataMember(Name = "landedFactor")]
    public double landedFactor { get; set; }

    [DataMember(Name = "elc")]
    public double elc { get; set; }

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

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

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

}

[DataContract]
public class RootObject
{
    [DataMember(Name = "CostingNegotiatedList")]
    public List<CostingNegotiated> CostingNegotiatedList { get; set; }
}

private RootObject GetWebServiceResult(string wUrl)
{

    HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
    httpWReq.Method = "GET";
    httpWReq.ContentType = "application/json";
    httpWReq.Timeout = 300000;
    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))
        {
            string r = reader.ReadToEnd();

            //Deserialize our JSON
            DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(RootObject));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(r));
            jsonResponse = (RootObject)sr.ReadObject(ms);
        }
    }
    //Output JSON parsing error
    catch (Exception e)
    {
        FailComponent(e.ToString());
    }
    return jsonResponse;

}

我已經驗證了字符串變量r具有JSON字符串。 當我到達這一行時:

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

jsonResponse具有RootObject,但是CostingNegotiatedList列表變量為NULL。 我需要怎么做才能填充此列表?

我想到了。 我的JSON沒有header元素,它只是一個直接列表,因此我不需要RootObject變量。 我更改了代碼以引用對象列表:

私有列表GetWebServiceResult(string wUrl){

HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
httpWReq.Method = "GET";
httpWReq.ContentType = "application/json";
httpWReq.Timeout = 300000;
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
List<CostingNegotiated> jsonResponse = null;

try
{
    //Get the stream of JSON
    Stream responseStream = httpWResp.GetResponseStream();

    //Deserialize the JSON stream
    using (StreamReader reader = new StreamReader(responseStream))
    {
        string r = reader.ReadToEnd();

        //Deserialize our JSON
        DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(List<CostingNegotiated>));
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(r));
        jsonResponse = (List<CostingNegotiated>)sr.ReadObject(ms);
    }
}
//Output JSON parsing error
catch (Exception e)
{
    FailComponent(e.ToString());
}
return jsonResponse; 

暫無
暫無

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

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