我正在从SSIS的脚本任务中制作一个非常简单的JSON请求。 URL返回非常大,有2000万个字符。
第一个网址返回行如下所示:
[{"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"},...
我通过解析器运行网址返回并验证其格式正确。
这是我的代码。
[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。我需要做些什么来填充此列表?
答案 0 :(得分:1)
我明白了。我的JSON没有头元素,它只是一个直列表,所以我不需要我的RootObject变量。我更改了代码以引用我的对象列表:
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;