从json windows phone解析字典 [英] Parse dictionary from json windows phone

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

问题描述

我有两段代码



Code1

$
  ; 

I have two piece of code

Code1

  

 string json = @"{""properties"":{""name"":""carl""}}";
    try
    {
        MemoryStream stream = GetMemoryStreamFromString(json);
        Type type = typeof(Person);
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);
        object obj = serializer.ReadObject(stream);
        Debug.WriteLine(obj);
        Person person = obj as Person;
    }
    catch (Exception ee)
    {
        Debug.WriteLine(ee.Message);
    }

    //And my classes
    [DataContract]
    public class Person
    {
        [DataMember]
        public Property properties { set; get; }

        public Person() { }
    }

    [DataContract]
    public class Property
    {
        [DataMember]
        public string name { set; get; }

        public Property() { }
    }










Code2



  





Code2

  

 string json = @"{""properties"":{""name"":""carl""}}";
    try
    {
        MemoryStream stream = GetMemoryStreamFromString(json);
        Type type = typeof(Person);
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);
        object obj = serializer.ReadObject(stream);
        Debug.WriteLine(obj);
        Person person = obj as Person;
    }
    catch (Exception ee)
    {
        Debug.WriteLine(ee.Message);
    }

    //And my class
    [DataContract]
    public class Person
    {
        [DataMember]
        public Dictionary<string,string> properties { set; get; }

        public Person() { }
    }










Code1工作正常,但Code2给了我例外。以下是日志

数据合同类型"ScrollViewExample.Person"无法反序列化,因为成员"属性"不公开。将该成员公开将修复此错误。或者,您可以将其设置为内部,并使用程序集上的InternalsVisibleToAttribute
属性以启用内部成员的序列化 - 有关更多详细信息,请参阅文档。请注意,这样做会产生一定的安全隐患。




$
这里的问题是我不知道t有一个为 属性定义的结构
它可以有任何键,所以我不能为它定义一个类。我发布这个问题是因为我有一个

问题
并在调查后发现我无法解析字典。所以我把我的问题变成了一个更简单的问题,这样你们就可以给你的输入了。


$





Code1 works fine but Code2 give me the exception. Here is the log
The data contract type 'ScrollViewExample.Person' cannot be deserialized because the member 'properties' is not public. Making the member public will fix this error. Alternatively, you can make it internal, and use the InternalsVisibleToAttribute attribute on your assembly in order to enable serialization of internal members - see documentation for more details. Be aware that doing so has certain security implications.


The problem here is that I don't have a structure defined for the properties it can have any keys so I can't define a class for it. I'm posting this question because I have a problem and after investigating I found that I can't parse dictionary. So I'm framing my problem into a simpler one so that you guys may give your inputs



推荐答案

您的Json数据的来源是什么?

What is the source of your Json data?

如果您反转流程并编写对象,则可以看到Dictionary< string,string>类型的属性输出Json如下:

If you reverse the process and write your object you can see that a property of type Dictionary<string, string> outputs Json like the following:

{" properties":[{" Key":" Name"," Value" :" Valorie"},{"关键":"月","价值":"五月"},{"关键":"年","价值":" 2013"} ]}

{"properties":[{"Key":"Name","Value":"Valorie"},{"Key":"Month","Value":"May"},{"Key":"Year","Value":"2013"}]}

你可以靠近List< string []> :
$
{" properties":[[" Name"," Valorie"],[" Month"," May"],[" Year"," 2013" ]]}

You can get closer with List<string[]> :
{"properties":[["Name","Valorie"],["Month","May"],["Year","2013"]]}


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

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