Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用JSON.net反序列化Xamarin表单中的JSON对象_Json_Xamarin.forms_Xamarin.android_Json.net_Cross Platform - Fatal编程技术网

无法使用JSON.net反序列化Xamarin表单中的JSON对象

无法使用JSON.net反序列化Xamarin表单中的JSON对象,json,xamarin.forms,xamarin.android,json.net,cross-platform,Json,Xamarin.forms,Xamarin.android,Json.net,Cross Platform,我在反序列化来自Android项目中API调用的JSON时遇到问题。然而,当我在单元测试项目中引用共享库(.NET标准策略)并测试代码时,一切都正常,我的模型填充正确。使用[Preserve(AllMembers=true)]没有帮助 这些是我的模型: public class StockQuotesResultDto { [JsonProperty("Meta Data")] public MetaData MetaData { get; set; } [JsonPr

我在反序列化来自Android项目中API调用的JSON时遇到问题。然而,当我在单元测试项目中引用共享库(.NET标准策略)并测试代码时,一切都正常,我的模型填充正确。使用[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);
这是我用来反序列化的代码:

{
    "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);
StockQuotesResultDto stockQuotes=JsonConvert.DeserializeObject(contentAsString);

当我在手机上调试应用程序时,StockQuotesResultDto的所有属性都为null(请注意,结果是从REST API调用中正确检索到的)。然而,当我调试执行相同代码的测试时,一切都很好。我刚开始和Xamarin比赛,我完全不知所措。我在这里和其他论坛上读了很多帖子,但没有找到解决方案。我将感谢任何帮助

目前我已经找到了解决问题的“办法”。 事实证明,只有当我通过Xamarin live link在设备上调试应用程序时,问题才会出现如果我在模拟器上运行它-一切正常。现在这是可以接受的,因为我只是随便玩玩。当它正确地部署在设备上时,它是否运行良好将是有趣的


编辑:在设备上调试时,如果没有Xamarin Live,也可以正常工作。事实证明,问题只会通过Xamarin Live link出现。

检查Android project中是否安装了json.net nuget,而不仅仅是共享项目。感谢您的输入。我错过了这一点,并且刚刚尝试了它,不幸的是它不起作用…所以当您在电话上调试时,JsonConvert.DeserializeObject不会抛出异常?不会抛出任何异常。只是结果对象的属性都是空值。在测试项目中,它们都是正确填充的…当你说“Xamarin Live Link”时,你是指Xamarin Live Player吗?是的,这就是我的意思,谢谢@ChrisNicksonPrime!我将与Xamarin Live Player团队内部分享您的反馈!