2017-05-27 70 views
1

以下是我从Web服务返回的JSON数据的示例。无法反序列化包含元数据和记录的当前JSON数组

它具有“odata.metadata”的单个节点,“value”的另一个单节点,然后是由3个字段“Code”,“Name”和“XTag”组成的重复记录。这是被称为内容

{"odata.metadata":"https://notthereal.domain:3148/system/OData/$metadata#pageLocationList","value":[{"Code":"LOC-A","Name":"Location A","XTag":"36;DgAAAAJ7/zEAMAAwAC0ATQBBAEkATgAAAAAA8;264943250;"},{"Code":"LOC-B","Name":"Location B","XTag":"36;DgAAAAJ7/zEAMAAxAC0ATQBBAEkATgAAAAAA8;388906690;"},{"Code":"LOC-C","Name":"Location C","XTag":"36;DgAAAAJ7/zEAMAAyAC0ATQBBAEkATgAAAAAA8;388844480;"},{"Code":"LOC-D","Name":"Location D","XTag":"36;DgAAAAJ7/zEAMAAzAC0ATQBBAEkATgAAAAAA8;388876720;"}]} 

我已经在为模型,我用JSON数据粘贴到类以下的字符串变量。

public class ModelPageLocationList 
{ 
    public string odatametadata { get; set; } 
    public List<Value> value { get; set; } 
} 

public class Value 
{ 
    public string Code { get; set; } 

    public string Name { get; set; } 

    public string XTag { get; set; } 
} 

,这是试图deserialise它的代码...

DataPageLocationList = 
JsonConvert.DeserializeObject<List<ModelPageLocationList>>(content); 

..和这样的错误消息。我卡住了!有任何想法吗?我认为问题出现在开始时的2个单个记录的周围,然后是重复的数据。

ERROR Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 

这里是我打电话

public async Task<List<ModelPageLocationList>> RefreshPageLocationListAsync() 
    { 
     DataPageLocationList = new List<ModelPageLocationList>(); 

     var uri = new Uri(string.Format(WebServiceSettings.WebServiceUrl, string.Empty)); 

     try 
     { 
      var response = await client.GetAsync(uri); 
      if (response.IsSuccessStatusCode) 
      { 
       var content = await response.Content.ReadAsStringAsync(); 
       Debug.WriteLine("{0}", content.ToString()); 

       DataPageLocationList = JsonConvert.DeserializeObject<List<ModelPageLocationList>>(content); 

      } 
     } catch (Exception ex) 
     { 
      Debug.WriteLine(@"ERROR {0}", ex.Message); 
     } 

     return (DataPageLocationList); 
    } 
+0

你可以分享你试图反序列化的JSON吗? –

+0

这是帖子的第一行 –

回答

0

你需要序列化到你的根对象ModelPageLocationList,而不是你的根对象列表。你的JSON是一个不是对象数组的对象。这将修复它:

DataPageLocationList = 
    JsonConvert.DeserializeObject<ModelPageLocationList>(content); 
+0

我收到一个错误“不能隐式地将类型'App.ModelPageLocationList'转换为'System.Collections.Generic.List ' –

0

没有看到您尝试传入的JSON数据,我只能根据您提供的信息提供两个可能的答案。

1)确保您的JSON是这样的:

{ 
    "odatametadata": "SomeDataHere", 
    "value": [{ 
      "code": "Code1", 
      "name": "Name1", 
      "xTag": "XTag1" 
     }, { 
      "code": "Code1", 
      "name": "Name1", 
      "xTag": "XTag1" 
     }, { 
      "code": "Code1", 
      "name": "Name1", 
      "xTag": "XTag1" 
     }, 
    ] 
} 

2这个属性添加到您的C#类模型

public class ModelPageLocationList 
{ 
    [JsonProperty("odatametadata")] 
    public string odatametadata { get; set; } 
    [JsonProperty("value")] 
    public List<Value> value { get; set; } 
} 

public class Value 
{ 
    [JsonProperty("code")] 
    public string Code { get; set; } 
    [JsonProperty("name")] 
    public string Name { get; set; } 
    [JsonProperty("xTag")] 
    public string XTag { get; set; } 
} 
+0

(这是帖子的第一行,我无法控制收到的数据)。 –

2

两件事情。使用JsonProperty来处理odata.metadata属性,因为属性名称中的点(.)将导致语法问题。

public class ModelPageLocationList { 
    [JsonProperty("odata.metadata")] 
    public string odatametadata { get; set; } 
    [JsonProperty("value")] 
    public List<Value> value { get; set; } 
} 
根据所提供的JSON示例数据

接下来,你需要重构调用代码

public async Task<ModelPageLocationList> RefreshPageLocationListAsync() { 
    try { 
     var uri = new Uri(string.Format(WebServiceSettings.WebServiceUrl, string.Empty)); 
     var response = await client.GetAsync(uri); 
     if (response.IsSuccessStatusCode) { 
      var result = await response.Content.ReadAsAsync<ModelPageLocationList>(); 
      return result; 
     } 
    } catch (Exception ex) { 
     Debug.WriteLine(@"ERROR {0}", ex.Message); 
    } 
    return null; 
} 
+0

我没有Content.ReadAsAsync作为选项。 –

+0

这是一个在System.Net.Http.Formatting.dll中找到的扩展方法, – Nkosi

+0

所以在你的解决方案中没有JsonConvert? –

0

我做了以下把我的代码在我的文章开始使用JSON数据的工作。不理想,我宁愿能够做到这一点,而无需编辑JSON字符串。

public async Task<List<ModelPageLocationList>> RefreshPageLocationListAsync() 
    { 
     DataPageLocationList = new List<ModelPageLocationList>(); 
     var uri = new Uri(string.Format(WebServiceSettings.WebServiceUrl, string.Empty)); 

     try 
     { 
      var response = await client.GetAsync(uri); 
      if (response.IsSuccessStatusCode) 
      { 
       var content = await response.Content.ReadAsStringAsync(); 
       Debug.WriteLine("BEFORE {0}", content.ToString()); 

       content = content.Substring(content.IndexOf('['), content.IndexOf(']') - content.IndexOf('[') + 1); 
       Debug.WriteLine("AFTER {0}", content.ToString()); 

       DataPageLocationList = JsonConvert.DeserializeObject<List<ModelPageLocationList>>(content); 
      } 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(@"ERROR {0}", ex.Message); 
     } 
     return (DataPageLocationList); 
    } 

和类从JSON数据

public class ModelPageLocationList 
{ 
    public string Code { get; set; } 

    public string Name { get; set; } 

    public string ETag { get; set; } 
} 

谢谢大家对他们的答复。