我有以下代码,其中我从服务获得结果如下:
var result=CallService();
response.Alllist = new List<Check>
{
new Check
{
Bundle1 = new Bundle
{
Documents = new List<Document>
{
new Document(), new Document()
}
},
},
new CheckList
{
Bundle1 = new Bundle
{
Documents = new List<Document>
{
new Document(), new Document()
}
},
}
我正在努力为此分配价值。 响应类是
public class Response
{
[DataMember(Order = 1)]
public bool Response { get; set; }
[DataMember(Order = 2)]
public List<Check> Alllist { get; set; }
}
public class Document
{
[DataMember(Order = 1)]
public string DocumentType { get; set; }
[DataMember(Order = 2)]
public string DocumentName { get; set; }
}
public class Bundle
{
[DataMember(Order = 1)]
public string BundleName { get; set; }
[DataMember(Order = 2)]
public string DocumentCategory { get; set; }
[DataMember(Order = 3)]
public string NextBundleName { get; set; }
[DataMember(Order = 4)]
public List<Document> Documents { get; set; }
}
public class Check
{
[DataMember(Order = 2)]
public string TransactionID { get; set; }
[DataMember(Order = 4)]
public Bundle Bundle1 { get; set; }
}
服务返回,带有多个实例的system.collection.generic.list的两个实例。并返回值 BUNDLENAME, DocumentCategory, NextBundleName, DocumentType, DocumentName。
如何获取结果值并分配给此响应? 我想像这样分配
int count=0;
foreach (var c in result)
{
response.Alllist[count].Bundle1.BundleName = c
}
但由于结果是动态的,我无法获取c.BundleName
的值答案 0 :(得分:2)
如果response.Alllist
是您在第一个代码块中演示的List<Check>
,则可以这种方式填充值:
response.Alllist[0].Bundle1.DocumentCategory = "my category";
response.Alllist[0].Bundle1.Documents[0].DocumentName = "my doc name";
等等。
除此之外,我真的不知道还能告诉你什么。我假设您知道通过索引等来处理集合。您只需要查看第二个代码块中的类组合层次结构,即哪些类包含实例或其他类的集合。
答案 1 :(得分:0)
根据我对你的问题的理解,在类似的情况下,我尝试了类似下面的内容。希望这可以帮助。 myclass是我的项目包含字段中的类定义。
Guid testGuid = guid.empty;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(testData.customerAccountURL).Result;
if (response.IsSuccessStatusCode)
{
string JSONResponse = response.Content.ReadAsStringAsync().Result;
var rObjects = JsonConvert.DeserializeObject<List<myclass>>(JSONResponse);
testGuid = Guid.Parse(rObjects.First().field1.ToString());
// now use this guid to search for a customer
}
string GuidURL = URL + "/"+ testGuid;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(GuidURL);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "GET";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
string data = values.ElementAt(0).Value;
}
}