Retrieve Dictionary Values from json File

2019-08-30 08:04发布

问题:

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());

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

回答1:

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;
    }

And I made a class

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