帮助JSON文件读取 [英] Help with JSON file reading

查看:75
本文介绍了帮助JSON文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的这个json文件位于我的Assets / Dec1.js。

如何让它出现在我的文本块中?

当我在模拟器中运行它时它显示This而不是显示Assets / Dec1.js中的内容





xaml

I have this json file located in my Assets/Dec1.js .
How can i make it appear in my textblock?
When i run it in the emulator it shows "This" instead of showing the content in the Assets/Dec1.js


xaml

<ScrollViewer>
                <TextBlock x:Name="textblock" TextWrapping="Wrap" Foreground="White"/>
            </ScrollViewer>







xaml.cs




xaml.cs

//JSON starts here
public string ReadJsonFile(string JsonfilePath)
{
    string strText = "This";

    using (StreamReader r = new StreamReader("Assets/Dec1.js"))
    {

        string json = r.ReadToEnd();
        dynamic array = JsonConvert.DeserializeObject(json);
        //... read text from json file


    }

    return strText;

}

//Daily Devotion starts here
protected override void OnNavigatedTo(NavigationEventArgs e)
{

    AddDevotions();
    int index = DateTime.Now.DayOfYear;
    textblock.Text = devotions[index];
}

List<String> devotions = new List<string>();
private void AddDevotions()
{
devotions.Add(ReadJsonFile("Assets/Dec1.js"));
    devotions.Add(ReadJsonFile("Assets/Dec1.js"));
    devotions.Add(ReadJsonFile("Assets/Dec1.js"));
    }







提前感谢您的回复




Thank you in advance and reply soon

推荐答案

在文件中使用JSON数据就像读取文件资源一样,然后在JSON文件中只需反序列化数据以创建该数据的对象,它就可以是匿名的,或者,您可以创建一个特殊的(自定义)类来处理通过该文件传入的数据。假设您要存储每日数据的数据。您可以创建一个类,



Working with JSON data in a file is just as reading the file resource, and then in the JSON file you just deserialize the data to create an object of that data, it can be anonymous or, you can create a special (custom) class for working with data coming through the file. Supposing that you're going to store the data for Daily data. You can create a class as,

class Devotion {
   public int DevotionID { get; set; }
   public int DevotionLabel { get; set; }
   // all other properties
}





..然后你可以使用Newtonsoft.JSON API为数据创建JSON表示法。例如,要读取对象,您应该创建这些对象的列表,





.. then you can use the Newtonsoft.JSON API to create the JSON notation for the data. For example, to read the objects, you should create a list of these objects as,

string jsonData = File.ReadAllText("/file.json"); // or that r.ReadToEnd()
List<devotion> devotions = new List<devotion>();
// make sure that the data in file starts with [ and ends at ]; because of List object
devotions = JsonConvert.DeserializeObject<list><devotion>>(jsonData);
</devotion></list></devotion></devotion>





现在,devotions对象是一个包含你所有日常奉献的可枚举对象。您可以使用foreach循环来使用这些对象中的每一个并在TextBlock中写入它们的值。相同的方法用于保存JSON文件中的值。



你可以从中了解更多...



使用Newtonsoft JSON序列化进行JSON序列化 [ ^ ]



JSON.NET的文档 [ ^ ]


这篇关于帮助JSON文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆