【问题标题】:How can I parse a JSON file from Assets folder in UWP - Closed如何从 UWP 中的 Assets 文件夹中解析 JSON 文件 - 已关闭
【发布时间】:2017-08-31 00:14:48
【问题描述】:

我是新开发的 Windows UWP 应用程序,我正在尝试从我的资产文件夹中解析 JSON 文件。我看过很多教程,但是当我尝试时,它们不起作用。请有人帮我解析它,如果可以的话,请举个例子。

我正在使用 VS2017 和 C#;

这是我要使用的库:

using Windows.Data.Json;

我的代码是:

private Uri appUri = new Uri("ms-appx:///Assets/marker.json");
private string title;
private void ConverJSONtoObjects()
        {
            try
            {
                Uri appUri = new Uri(fileName);//File name should be prefixed with 'ms-appx:///Assets/* 
                StorageFile anjFile = StorageFile.GetFileFromApplicationUriAsync(appUri).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); 
                string jsonText = FileIO.ReadTextAsync(anjFile).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); 
                JsonArray obj = JsonValue.Parse(jsonText).GetArray();
                for (uint i = 0; i < obj.Count; i++)
                {
                    title = obj.GetObjectAt(i).GetNamedString("name");
                }
                message("Place", title);
            }
            catch (Exception ex)
            {
                message("Error", ex.ToString());
            }
            
        }

我收到此错误: error handled by Exception

我的文件是这样的:

[
{
"id":1,
"name":"Cabañas Nuevo Amanecer",
"lat":"18.402785",
"lng":"-70.094953",
"type":"Normal",
"phone":"No Disponible",
"price":"DOP 500",
"image":"http://i65.tinypic.com/10mif69.jpg"
},
{
"id":2,
"name":"Cabañas Costa Azul",
"lat":"18.424746",
"lng":" -69.990333",
"type":"Lujosa",
"phone":"(809) 539-6969",
"price":"DOP 4453",
"image":"http://i64.tinypic.com/wcd5b8.png"
}
]

解决方案(2021/11/7 更新)

您好,对于那些有同样问题的人,问题出在文件属性中:构建操作需要是 Content 才能从 UWP 中的 Assets 文件夹中调用文件 p>

【问题讨论】:

  • 你应该提供更多关于你不明白的细节,因为如果你只是想要解析,它就像 Windows.Data.Json.JArray.Parse(jsonString); 一样简单。
  • @CyprienAutexier 让我们看看这是否对您有帮助
  • 那段代码有什么问题呢? (除了标题没有声明并且消息在循环之外并且总是显示最后一个值)
  • 我收到此错误:i68.tinypic.com/67o3zc.png
  • 谢谢,问题出在文件属性中。构建操作需要是 Content

标签: c# json parsing uwp jsonparser


【解决方案1】:

您可能正在寻找无数 JSON 库之一。您应该从Json.Net 开始,这是最受欢迎的选择。 (您可以查看Service Stack TextFastJsonParserJil 等替代方案。

一种简单的方法是声明一个与您预期的数据架构匹配的类:

public class PointOfInterest
{
    public int Id { get; set; }
    public string Name { get; set; }
    // ...
}

以及使用反序列化。:

var poiArray = JsonConvert.DeserializeObject<PointOfInterest[]>(jsonString, new JsonSerializerSettings 
    { 
        ContractResolver = new CamelCasePropertyNamesContractResolver() 
    });

使用更新后的要求进行编辑,您必须执行以下操作:

var array = JArray.Parse(jsonString);

foreach(JObject item in array){
   var poi = new PointOfInterest()
   poi.Id = (int)item.GetNamedNumber("id");
   //...
}

official documentation 非常简单。

【讨论】:

  • 谢谢,唯一的问题是我不想使用 Windows 的外部库,除非这是我最后的选择。我可以让它与 HTTPClient 请求一起工作,但我希望它在我的资产文件夹中工作
  • @MisterJJ 然后您可以使用 DataContractSerializer msdn.microsoft.com/en-us/library/… 但是:这是垃圾(功能支持非常有限且速度很慢)。例如,Miscrosoft AspNet Core 团队选择了 Json.Net 来支持 Json。
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2013-09-02
  • 2013-07-12
  • 2018-02-20
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
相关资源
最近更新 更多