我有一个简单的JSON键/值列表,通过POST发送回ASP.NET。例子:
{ "key1": "value1", "key2": "value2"}
我没有试图反序列化为强类型的.NET对象
我只需要一个普通的旧字典(Of String,String),或者一些等价的东西(哈希表,字典(OfString,Object),老式的StringDictionary——见鬼,一个二维字符串数组对我来说很有用。
我可以使用ASP.NET 3.5中可用的任何东西,也可以使用流行的Json.NET(我已经在使用它对客户端进行序列化)。
显然,这两个JSON库都没有这种开箱即用的能力——它们完全专注于通过强契约实现基于反射的反序列化。
有什么想法吗?
限制:
我不想实现我自己的JSON解析器尚无法使用ASP.NET 4.0希望远离旧的、不推荐使用的JSON ASP.NET类
我刚刚在RestSharp中实现了这一点。这篇文章对我很有帮助。
除了链接中的代码,这是我的代码。现在,当我这样做时,我会得到一个结果字典:
var jsonClient = new RestClient(url.Host);
jsonClient.AddHandler("application/json", new DynamicJsonDeserializer());
var jsonRequest = new RestRequest(url.Query, Method.GET);
Dictionary<string, dynamic> response = jsonClient.Execute<JObject>(jsonRequest).Data.ToObject<Dictionary<string, dynamic>>();
请注意您期望的JSON类型——在我的例子中,我检索的是一个具有多个财产的单个对象。在所附的链接中,作者正在检索列表。
编辑:这是可行的,但使用Json.NET的公认答案要简单得多。留下这个,以防有人只需要BCL代码。
现成的.NET框架不支持它。一个明显的疏忽——并不是每个人都需要反序列化为具有命名财产的对象。所以,我最终推出了自己的:
VB.NET:
<Serializable()> Public Class StringStringDictionary
Implements ISerializable
Public dict As System.Collections.Generic.Dictionary(Of String, String)
Public Sub New()
dict = New System.Collections.Generic.Dictionary(Of String, String)
End Sub
Protected Sub New(info As SerializationInfo, _
context As StreamingContext)
dict = New System.Collections.Generic.Dictionary(Of String, String)
For Each entry As SerializationEntry In info
dict.Add(entry.Name, DirectCast(entry.Value, String))
Next
End Sub
Public Sub GetObjectData(info As SerializationInfo, context As StreamingContext) Implements ISerializable.GetObjectData
For Each key As String in dict.Keys
info.AddValue(key, dict.Item(key))
Next
End Sub
End Class
与C#相同:
public class StringStringDictionary : ISerializable
{
public System.Collections.Generic.Dictionary<string, string> dict;
public StringStringDictionary()
{
dict = new System.Collections.Generic.Dictionary<string, string>();
}
protected StringStringDictionary(SerializationInfo info, StreamingContext context)
{
dict = new System.Collections.Generic.Dictionary<string, string>();
foreach (SerializationEntry entry in info)
dict.Add(entry.Name, (string)entry.Value);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (string key in dict.Keys)
info.AddValue(key, dict[key]);
}
}
调用方式:
string MyJsonString = "{ \"key1\": \"value1\", \"key2\": \"value2\"}";
System.Runtime.Serialization.Json.DataContractJsonSerializer dcjs = new
System.Runtime.Serialization.Json.DataContractJsonSerializer(
typeof(StringStringDictionary));
System.IO.MemoryStream ms = new
System.IO.MemoryStream(Encoding.UTF8.GetBytes(MyJsonString));
StringStringDictionary myfields = (StringStringDictionary)dcjs.ReadObject(ms);
Response.Write("Value of key2: " + myfields.dict["key2"]);
很抱歉,C#和VB.NET混合使用…
我将JSON中的空值检查添加到另一个答案中
我也有同样的问题,所以我自己写了这个。此解决方案是与其他答案不同,因为它可以反序列化为多个级别。
只需将json字符串发送到反序列化ToDictionary函数将返回非强类型Dictionary<string,object>object。
private Dictionary<string, object> deserializeToDictionary(string jo)
{
var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(jo);
var values2 = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> d in values)
{
if (d.Value != null && d.Value.GetType().FullName.Contains("Newtonsoft.Json.Linq.JObject"))
{
values2.Add(d.Key, deserializeToDictionary(d.Value.ToString()));
}
else
{
values2.Add(d.Key, d.Value);
}
}
return values2;
}
示例:这将返回Facebook的Dictionary<string,object>objectJSON响应。
private void button1_Click(object sender, EventArgs e)
{
string responsestring = "{\"id\":\"721055828\",\"name\":\"Dasun Sameera
Weerasinghe\",\"first_name\":\"Dasun\",\"middle_name\":\"Sameera\",\"last_name\":\"Weerasinghe\",\"username\":\"dasun\",\"gender\":\"male\",\"locale\":\"en_US\",
hometown: {id: \"108388329191258\", name: \"Moratuwa, Sri Lanka\",}}";
Dictionary<string, object> values = deserializeToDictionary(responsestring);
}
注意:home进一步反序列化为Dictionary<string,object>对象。
根据上面的评论,尝试JsonConvert.DeserializeObject<Dictionary<string,dynamic>(json)
var json = @"{""key1"":1,""key2"":""value2"", ""object1"":{""property1"":""value1"",""property2"":[2,3,4,5,6,7]}}";
var parsedObject = JsonConvert.DeserializeObject<Dictionary<string,dynamic>>(json);
似乎甚至适用于复杂的对象和列表。
我只需要解析一个嵌套的字典
{
"x": {
"a": 1,
"b": 2,
"c": 3
}
}
其中JsonConvert.DeserializeObject没有帮助。我发现了以下方法:
var dict = JObject.Parse(json).SelectToken("x").ToObject<Dictionary<string, int>>();
SelectToken允许您深入到所需的字段。您甚至可以指定一个类似“x.y.z”的路径,以进一步深入JSON对象。