从 Web 请求序列化 JSON 数组 [英] Serialize JSON array from a web request

查看:18
本文介绍了从 Web 请求序列化 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 SSIS 的脚本任务发出一个非常简单的 JSON 请求.URL 返回非常大,2000 万个字符.

I am making what should be a very simple JSON request from a script task from SSIS. The URL return is very large, 20 million characters.

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

The first URL return line looks like this:

[{"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 返回,并验证它的格式正确.

I've run the URL return through a parser and verified that it is well-formed.

这是我的代码.

[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 字符串.当我到达这条线时:

I've verified that the string variable r has the JSON string. When I get to this line:

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

jsonResponse 有一个 RootObject,但 CostingNegotiatedList 列表变量为 NULL.我需要做什么才能填充此列表?

The jsonResponse has a RootObject, but the CostingNegotiatedList list variable is NULL. What do I need to do to get this list to populate?

推荐答案

我想通了.我的 JSON 没有 header 元素,它只是一个直接列表,所以我不需要我的 RootObject 变量.我更改了代码以引用我的对象列表:

I figured it out. My JSON doesn't have a header element, it's just a straight list, so I don't need my RootObject variable. I changed my code to refer to a list of my objects:

私有列表 GetWebServiceResult(string wUrl){

private List 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; 

这篇关于从 Web 请求序列化 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆