在我的学校 uwp 项目中读取本地 Json 文件时遇到问题

问题描述 投票:0回答:1

我正在为我的一篇班级论文创建一个小的 UWP 应用程序,并试图读取我创建并使用 jsonlint 验证的 json 文件。如果您还不能告诉我对 uwp 有点菜鸟,有人可以帮我指出正确的方向以寻找解决方案吗?

我正在尝试读取 json 并将内容放入列表框中。问题是我继续出错。

Newtonsoft.Json.dll 中出现 'Newtonsoft.Json.JsonReaderException' 类型的异常,但未在用户代码中处理 解析值时遇到意外字符:{。路径 '',第 2 行,位置 3.

我觉得我的代码隐藏是正确的,我的 json 是正确的,但显然不是。

这是隐藏代码

private async void LoadData()
        {
            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///questList.json"));

            string jsonText = await FileIO.ReadTextAsync(file);

            List<string> items = JsonConvert.DeserializeObject<List<string>>(jsonText);
            foreach (string item in items)
            {
                questList.Items.Add(item);
            }
        }

这是json

[
  {
    "taderName": "Prapor",
    "quests": [
      {
        "title": "Debut",
        "objectives": [
          {
            "objective": "Eliminate 5 scavs all over the Tarkov Territory"
          },
          {
            "objective": "Obtain and hand over 2 MP-133 12ga shotguns"
          }
        ]
      }
    ]
  },
  {
    "taderName": "therapist",
    "quests": [
      {
        "title": "derka",
        "objectives": [
          {
            "objective": "Eliminate 5 scavs all over the Tarkov Territory"
          },
          {
            "objective": "Obtain and hand over 2 MP-133 12ga shotguns"
          }
        ]
      }
    ]
  }
]

如果有人能指出我正确的方向,那就太好了,提前谢谢你。

uwp
1个回答
0
投票

您的 JSON 字符串由具有相同结构的项目组成。它不是字符串对象,因此您不能使用字符串作为类型。请创建您自己的类,对应于 JSON 的项目结构。

请检查以下代码。我创建了一个

Root
类、一个
Quest
类和一个
Objective
类来处理 JSON 值。

    public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string jsonText = "[\r\n  {\r\n    \"taderName\": \"Prapor\",\r\n    \"quests\": [\r\n      {\r\n        \"title\": \"Debut\",\r\n        \"objectives\": [\r\n          {\r\n            \"objective\": \"Eliminate 5 scavs all over the Tarkov Territory\"\r\n          },\r\n          {\r\n            \"objective\": \"Obtain and hand over 2 MP-133 12ga shotguns\"\r\n          }\r\n        ]\r\n      }\r\n    ]\r\n  },\r\n  {\r\n    \"taderName\": \"therapist\",\r\n    \"quests\": [\r\n      {\r\n        \"title\": \"derka\",\r\n        \"objectives\": [\r\n          {\r\n            \"objective\": \"Eliminate 5 scavs all over the Tarkov Territory\"\r\n          },\r\n          {\r\n            \"objective\": \"Obtain and hand over 2 MP-133 12ga shotguns\"\r\n          }\r\n        ]\r\n      }\r\n    ]\r\n  }\r\n]";

        List<Root> items = JsonConvert.DeserializeObject<List<Root>>(jsonText);
        foreach (Root item in items)
        {
            Debug.WriteLine(item.taderName);
        }
    }
}

public class Objective
{
    public string objective { get; set; }
}

public class Quest
{
    public string title { get; set; }
    public List<Objective> objectives { get; set; }
}

public class Root
{
    public string taderName { get; set; }
    public List<Quest> quests { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.