尝试从根PCL Xamarin加载JSON时为Null

时间:2019-02-11 11:14:43

标签: c# xamarin

您好,当尝试从我的PCL根目录加载JSON文件时,它在此行使用(var reader = new StreamReader(stream))中断,并说其为空。

这是loadJson()的完整方法

public void LoadJson()
{
    //Loads the JSON File in the Solution and Finds Correct ID of Accordion Hopefully!!
    var assembly = typeof(App).GetTypeInfo().Assembly;
    Stream stream = assembly.GetManifestResourceStream("MCETimeTest.TimeSheet.json");

    string jsonString = "";
    using (var reader = new StreamReader(stream))
    {
        jsonString = reader.ReadToEnd();
    };

    uoObj = JsonConvert.DeserializeObject<RootObject>(jsonString);
}

编辑:单步执行代码流时始终为空

我的json类是:

 //JSON Classes
    public class RootObject
    {
        public List<LineItem> LineItems { get; set; }
    }

    public class Checks
    {
        public DateTime TheDate { get; set; }
        public string JobNumber { get; set; }
        public string CustomerName { get; set; }
        public TimeSpan On1 { get; set; }
        public TimeSpan Off1 { get; set; }
        public TimeSpan On2 { get; set; }
        public TimeSpan Off2 { get; set; }
        public string Description { get; set; }
        public string SingleHours { get; set; }
        public string TimeHalfHours { get; set; }
        public string DoubleHours { get; set; }

    }

    public class LineItem
    {
        public string Id { get; set; }
        public string Customer { get; set; }
        public List<Checks> Checks { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

也查看解决方案,结果发现根目录中的JSON文件不是嵌入式资源。更改后,它可以工作!麻烦您了!