0
我有一個爲uwp編寫的c#客戶端。在UWP中將json對象轉換爲XML
它有一個REST API客戶端。
服務器代碼是這樣的:
*API Controller*
[HttpGet]
public IEnumerable<Services.Group> Get(Guid companyRef)
{
return groupRepository.Get(companyRef);
}
模型是這樣的:
public class Group
{
public int GroupId { get; set; }
public Guid GroupRef { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Guid CompanyRef { get; set; }
public bool Active { get; set; }
}
我的客戶是這樣的:
Uri uri = new Uri(Shared.URL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Shared.HeaderType));
HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?" + GeneralTags.COMPANY_REF + "=" + ApplicationObject.CompanyRef);
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
var objs = JArray.Parse(json); // parse as array
ApplicationObject.GroupData = objs.Select(x => new Model.Group
{
Description = (string)x[GeneralTags.DESCRIPTION],
GroupRef = (Guid)shared.CheckForNulls(x[GeneralTags.GROUP_REF], typeof(Guid)),
Name = (string)x[GeneralTags.NAME]
}).ToList();
不過,我現在更換:
ApplicationObject.GroupData = objs.Select(x => new Model.Group
{
Description = (string)x[GeneralTags.DESCRIPTION],
GroupRef = (Guid)shared.CheckForNulls(x[GeneralTags.GROUP_REF], typeof(Guid)),
Name = (string)x[GeneralTags.NAME]
}).ToList();
與此所以數據都包含在一個XML文檔而不是在:
var doc = JsonConvert.DeserializeObject(json, typeof(XmlDocument));
,但我得到了這樣一個錯誤:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Windows.Data.Xml.Dom.XmlDocument' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
雖然我明白了什麼錯誤消息告訴我,我不確定如何實現修復/更改
嗨,這將是一個2步驟的過程。我可以實現相同的解析ApplicationObject.GroupData –