从 json 文件中检索字典值 [英] Retrieve Dictionary Values from json File

查看:46
本文介绍了从 json 文件中检索字典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析从文件流中获取的 json 文件.以下是我的json数据

I am trying to parse a json file which is been got from a file stream. Following is my json data

{
   "appname":"sine",
    "taborder":  [
            "some",
            "thing",
            "is",
            "went",
            "wrong"  ]
}

我将数据存储在字符串中并尝试反序列化数据.我正在尝试以下列方式显示警报框中的键

I am storing the data in a String and trying to deserialize the data. I am trying to show the keys in alert box in the following way

 string jsonString = contents;//"{'Name':'Bill', 'Ag:53}";
 you deserializedUser = ReadToObject(jsonString);

 var str = deserializedUser.mainDict.Keys.ToArray();
 MessageBox.Show(str.ToString());

但是我得到的键值为null",如何正确获取键值,请帮助我.....

But i am getting the key value as "null", How to get the key values in proper , pls help me.....

推荐答案

我做了这个,希望能帮到你

I did this hope this helps

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
    JavaScriptRequest obj= DeserializeJavaScriptRequest(typeof(JavaScriptRequest), Resource1.test) as JavaScriptRequest;
    MessageBox.Show(obj.appname +" | " + obj.taborder[0]);
}

public object DeserializeJavaScriptRequest(Type typedeserialize, string eValue)
        {

            Type t = typedeserialize;
        // Get constructor info.
        ConstructorInfo[] ci = t.GetConstructors();           
        object reflectOb = ci[0].Invoke(null);
        MemoryStream confirm_ms = new MemoryStream(Encoding.UTF8.GetBytes(eValue));
        DataContractJsonSerializer confirm_ser = new DataContractJsonSerializer(typedeserialize);
        reflectOb = confirm_ser.ReadObject(confirm_ms);
        confirm_ms.Close();

        return reflectOb;
    }

我做了一堂课

 public class JavaScriptRequest
    {
        public string appname { get; set; }
        public string[] taborder { get; set; }
    }

这篇关于从 json 文件中检索字典值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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