无法使用 JSON.net 反序列化 Xamarin Forms 中的 JSON 对象
Cannot deserialize a JSON object in Xamarin Forms with JSON.net
我在反序列化来自我的 Android 项目中的 API 调用的 JSON 时遇到问题。然而,当我在单元测试项目中引用共享库(.NET Standard 策略)并测试代码时 - 一切正常并且我的模型已正确填充。使用 [Preserve(AllMembers = true)] 没有帮助。
这些是我的模型:
public class StockQuotesResultDto
{
[JsonProperty("Meta Data")]
public MetaData MetaData { get; set; }
[JsonProperty("Stock Quotes")]
public StockQuoteDto[] StockQuotes { get; set; }
}
//[Preserve(AllMembers = true)]
public class MetaData
{
[JsonProperty("1. Information")]
public string Information { get; set; }
[JsonProperty("2. Notes")]
public string Notes { get; set; }
[JsonProperty("3. Time Zone")]
public string TimeZone { get; set; }
}
//[Preserve(AllMembers = true)]
public class StockQuoteDto
{
[JsonProperty("1. symbol")]
public string Symbol { get; set; }
[JsonProperty("2. price")]
public string Price { get; set; }
[JsonProperty("3. volume")]
public string Volume { get; set; }
[JsonProperty("4. timestamp")]
public string Timestamp { get; set; }
}
这是我正在尝试反序列化的JSON:
{
"Meta Data": {
"1. Information": "Batch Stock Market Quotes",
"2. Notes": "IEX Real-Time Price provided for free by IEX (https://iextrading.com/developer/).",
"3. Time Zone": "US/Eastern"
},
"Stock Quotes": [
{
"1. symbol": "MSFT",
"2. price": "100.2500",
"3. volume": "--",
"4. timestamp": "2018-06-22 15:59:57"
},
{
"1. symbol": "FB",
"2. price": "201.4100",
"3. volume": "--",
"4. timestamp": "2018-06-22 15:59:59"
},
{
"1. symbol": "AAPL",
"2. price": "185.3900",
"3. volume": "--",
"4. timestamp": "2018-06-22 16:01:08"
}
]
}
这是我用来反序列化的代码:
StockQuotesResultDto stockQuotes = JsonConvert.DeserializeObject<StockQuotesResultDto>(contentAsString);
当我在 phone 上调试我的应用程序时,StockQuotesResultDto 的所有属性都为空(请注意,结果是从 REST API 调用中正确检索的)。然而,当我调试执行完全相同代码的测试时 - 一切都很好。我刚开始玩 Xamarin,完全不知所措。我在这里和其他论坛阅读了一堆主题,但找不到解决方案。我将不胜感激任何帮助!
目前我发现 "solution" 我的问题。
事实证明,只有当我通过 Xamarin live link 在我的设备上调试我的应用程序时才会出现问题。 如果我在模拟器上 运行 - 一切正常。这暂时可以接受,因为我只是在玩。当它正确部署在设备上时,它是否 运行 会很有趣。
编辑:在设备上调试时,但没有 Xamarin Live,它也能正常工作。事实证明,问题仅通过 Xamarin Live link.
发生
我在反序列化来自我的 Android 项目中的 API 调用的 JSON 时遇到问题。然而,当我在单元测试项目中引用共享库(.NET Standard 策略)并测试代码时 - 一切正常并且我的模型已正确填充。使用 [Preserve(AllMembers = true)] 没有帮助。
这些是我的模型:
public class StockQuotesResultDto
{
[JsonProperty("Meta Data")]
public MetaData MetaData { get; set; }
[JsonProperty("Stock Quotes")]
public StockQuoteDto[] StockQuotes { get; set; }
}
//[Preserve(AllMembers = true)]
public class MetaData
{
[JsonProperty("1. Information")]
public string Information { get; set; }
[JsonProperty("2. Notes")]
public string Notes { get; set; }
[JsonProperty("3. Time Zone")]
public string TimeZone { get; set; }
}
//[Preserve(AllMembers = true)]
public class StockQuoteDto
{
[JsonProperty("1. symbol")]
public string Symbol { get; set; }
[JsonProperty("2. price")]
public string Price { get; set; }
[JsonProperty("3. volume")]
public string Volume { get; set; }
[JsonProperty("4. timestamp")]
public string Timestamp { get; set; }
}
这是我正在尝试反序列化的JSON:
{
"Meta Data": {
"1. Information": "Batch Stock Market Quotes",
"2. Notes": "IEX Real-Time Price provided for free by IEX (https://iextrading.com/developer/).",
"3. Time Zone": "US/Eastern"
},
"Stock Quotes": [
{
"1. symbol": "MSFT",
"2. price": "100.2500",
"3. volume": "--",
"4. timestamp": "2018-06-22 15:59:57"
},
{
"1. symbol": "FB",
"2. price": "201.4100",
"3. volume": "--",
"4. timestamp": "2018-06-22 15:59:59"
},
{
"1. symbol": "AAPL",
"2. price": "185.3900",
"3. volume": "--",
"4. timestamp": "2018-06-22 16:01:08"
}
]
}
这是我用来反序列化的代码:
StockQuotesResultDto stockQuotes = JsonConvert.DeserializeObject<StockQuotesResultDto>(contentAsString);
当我在 phone 上调试我的应用程序时,StockQuotesResultDto 的所有属性都为空(请注意,结果是从 REST API 调用中正确检索的)。然而,当我调试执行完全相同代码的测试时 - 一切都很好。我刚开始玩 Xamarin,完全不知所措。我在这里和其他论坛阅读了一堆主题,但找不到解决方案。我将不胜感激任何帮助!
目前我发现 "solution" 我的问题。 事实证明,只有当我通过 Xamarin live link 在我的设备上调试我的应用程序时才会出现问题。 如果我在模拟器上 运行 - 一切正常。这暂时可以接受,因为我只是在玩。当它正确部署在设备上时,它是否 运行 会很有趣。
编辑:在设备上调试时,但没有 Xamarin Live,它也能正常工作。事实证明,问题仅通过 Xamarin Live link.
发生