【问题标题】:Xamarin forms: How to parse local JSON data file stored in the project?Xamarin 表单:如何解析存储在项目中的本地 JSON 数据文件?
【发布时间】:2022-01-17 15:54:48
【问题描述】:

我有一个本地 JSON 文件。我需要解析该文件中的数据并需要一个包含日期、颜色和消息详细信息的列表。

JSON 文件中的数据格式:

{"01-01-2017":{"color":"white","message":"The Octave Day of Christmas Solemnity of the Blessed Virgin Mary, the Mother of God Lectionary: 18"},"02-01-2017":{"color":"white","message":"Memorial of Saints Basil the Great and Gregory Nazianzen, Bishops and Doctors of the Church Lectionary: 205",.......}}

模型类

public class JsonContent
{
    public string date { get; set; }
    public string color { get; set; }
    public string message { get; set; }
}

我尝试了以下代码:

string jsonString;
string jsonFileName = "Files.prayers.json";
var assembly = typeof(HomePage1).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{jsonFileName}");
using (var reader = new System.IO.StreamReader(stream))
{
     jsonString = reader.ReadToEnd();
}
List <JsonContent> newList = new List<JsonContent>();
//I need to add all the items into the above list

如何获取包含日期、颜色和消息详细信息的列表?在此之后,我需要在XamForms.Enhanced.Calendar 上用颜色显示日期。我知道如何在日历中显示特殊日期,但停留在这个数据解析上。

【问题讨论】:

  • @Jason 我们已经更新了这个问题的更多细节,你能看看吗?

标签: xamarin.forms


【解决方案1】:

docs

我会尝试这样的事情

Dictionary<string, JsonContent> jsonData = JsonConvert.DeserializeObject<Dictionary<string, JsonContent>>(jsonString);
foreach (var item in jsonData)
{
    Debug.WriteLine("Date:>>" + item.Key);
    Debug.WriteLine("color:>>" + item.Value.color);
    Debug.WriteLine("message:>>" + item.Value.message);
}

【讨论】:

  • 从这本词典我如何解析日期、颜色和消息?你能分享一个示例颂歌吗?
  • color 和 message 是您创建的 JsonContent 类的属性。日期是字典的键。我不知道是否有一种简单的方法可以自动解析它。您可能需要对数据进行后处理才能转换日期。
  • 成功了.......
猜你喜欢
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多