这是可能的JSON? [英] Is this possible in JSON?

查看:127
本文介绍了这是可能的JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前与我json.net工作!我知道如何反序列化JSON数据和如何映射我们的类。现在我渴望一些疑问!

I am currently working with json.net ! I know how to deserialize json data and how to map with our class. Now I am eager about some queries !

假如我是jsonstrings

Suppose my jsonstrings is

"attributes":{
            "color":"Brown",
            "condition":"Used",
            "currency":"USD",
            "price":"10500",
            "price_display":"$10,500",
         }

和我的属性类(中,我想反序列化上面的字符串的)是

Public class Attribute
{
        public string name{ get; set; }
        public string value{ get; set; }
}

现在我使用以下code反序列化!

Now I am using following code for deserialization.!

 JavaScriptSerializer ser = new JavaScriptSerializer();
 <classname> p = ser.Deserialize<classname>(jsonString);

和这显然code未将以上jsonstring在所有的工作。

And Obviously this code is not going to work for above jsonstring at all.

现在我怎样才能在一个类绑定这些名称值对?这可能吗?

Now How can I bind these name value pair in one class ?? Is this possible ?

今后,属性可能B一次以上10,但格式会出现相同名称值对

推荐答案

编辑:我很抱歉,但不知何故,我间preTED你的问题,如果你想的连载,而不是反序列化(但我会继续低于反正原来,因为我认为这是很好的了解:))

I'm sorry, but somehow I interpreted your question as if you wanted to serialize, and not deserialize (but I'll keep the original below anyway, as I think it's good knowledge :)).

尝试反序列化JSON字符串词典&LT;字符串,字符串&GT; ,而不是你的属性的。我有一种预感,它可能工作。然后,您可以调整code的其余部分使用字典,也可以转换成你的属性实例的集合。

Try to deserialize your JSON string to Dictionary<string,string> instead of your Attribute. I have a hunch it might work. Then, you can adjust the rest of your code to use that dictionary or you can convert to collection of your Attribute instances.

最初的答案(相关的序列,这是你问的对面)以下。

The original answer (related to serializing, which was the opposite of what you asked for) below.

我不这么认为,但如果目的是用它在客户端上是这样的:

I don't think so, but if the intention is to use it on the client this way:

attributes["color"] // returns Brown
attributes["price"] // returns 10500

...那么你就被称为关联数组之后,因为它等同于请问这是什么:

... then what you're after is called "associative array", as it's equivalent to this:

attributes.color
attributes.price

...因为它不是一个真正的数组,但它是一个对象(JavaScript提供了两个引用对象属性的方法,如的window.onload 相同窗口[的onload] )...

...如果你序列词典&LT你可以用的JavaScriptSerializer实现这一目标;字符串,字符串&GT; 来JSON字符串(换句话说,改变你的属性 KeyValuePair&LT;字符串,字符串方式&gt; ,或以其他方式创造这样的字典不同的

... and you can achieve this with the JavaScriptSerializer if you serialize Dictionary<string,string> to JSON string (in other words, change your Attribute class to KeyValuePair<string,string>, or otherwise create such dictionary differently.

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

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