【发布时间】: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