var json = "{\"id\":\"13\", \"value\": true}";
var jss = new JavaScriptSerializer();
var table = jss.Deserialize(json);
Console.WriteLine(table["id"]);
Console.WriteLine(table["value"]);
4> 小智..:
试图不使用任何外部JSON实现,所以我反序列化如下:
string json = "{\"id\":\"13\", \"value\": true}";
var serializer = new JavaScriptSerializer(); //using System.Web.Script.Serialization;
Dictionary values = serializer.Deserialize>(json);
private Dictionary deserializeToDictionary(string jo)
{
var values = JsonConvert.DeserializeObject>(jo);
var values2 = new Dictionary();
foreach (KeyValuePair d in values)
{
// if (d.Value.GetType().FullName.Contains("Newtonsoft.Json.Linq.JObject"))
if (d.Value is JObject)
{
values2.Add(d.Key, deserializeToDictionary(d.Value.ToString()));
}
else
{
values2.Add(d.Key, d.Value);
}
}
return values2;
}
private static object deserializeToDictionaryOrList(string jo,bool isArray=false)
{
if (!isArray)
{
isArray = jo.Substring(0, 1) == "[";
}
if (!isArray)
{
var values = JsonConvert.DeserializeObject>(jo);
var values2 = new Dictionary();
foreach (KeyValuePair d in values)
{
if (d.Value is JObject)
{
values2.Add(d.Key, deserializeToDictionary(d.Value.ToString()));
}
else if (d.Value is JArray)
{
values2.Add(d.Key, deserializeToDictionary(d.Value.ToString(), true));
}
else
{
values2.Add(d.Key, d.Value);
}
}
return values2;
}else
{
var values = JsonConvert.DeserializeObject>(jo);
var values2 = new List
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
public Dictionary ParseJSON(string json)
{
int end;
return ParseJSON(json, 0, out end);
}
private Dictionary ParseJSON(string json, int start, out int end)
{
Dictionary dict = new Dictionary();
bool escbegin = false;
bool escend = false;
bool inquotes = false;
string key = null;
int cend;
StringBuilder sb = new StringBuilder();
Dictionary child = null;
List
string jsonReponseString= wClient.DownloadString(requestUrl);
IDictionary dict = new JavaScriptSerializer().DeserializeObject(jsonReponseString) as IDictionary;
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
叫:
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的混合......
[TestMethod] public void TestSimpleObject(){const string json = @"{""Name"":""Bob"",""Age"":42}"; var dict = new JavaScriptSerializer().DeserializeObject(json)as IDictionary ; Assert.IsNotNull(字典); Assert.IsTrue(dict.ContainsKey( "名称")); Assert.AreEqual("Bob",dict ["Name"]); Assert.IsTrue(dict.ContainsKey( "时代")); Assert.AreEqual(42,dict ["Age"]); }
///
/// Deserialize the given JSON string data () into a
/// dictionary.
///
/// JSON string.
/// Deserialized dictionary.
private IDictionary DeserializeData(string data)
{
var values = JsonConvert.DeserializeObject>(data);
return DeserializeData(values);
}
///
/// Deserialize the given JSON object () into a dictionary.
///
/// JSON object.
/// Deserialized dictionary.
private IDictionary DeserializeData(JObject data)
{
var dict = data.ToObject>();
return DeserializeData(dict);
}
///
/// Deserialize any elements of the given data dictionary ()
/// that are JSON object or JSON arrays into dictionaries or lists respectively.
///
/// Data dictionary.
/// Deserialized dictionary.
private IDictionary DeserializeData(IDictionary data)
{
foreach (var key in data.Keys.ToArray())
{
var value = data[key];
if (value is JObject)
data[key] = DeserializeData(value as JObject);
if (value is JArray)
data[key] = DeserializeData(value as JArray);
}
return data;
}
///
/// Deserialize the given JSON array () into a list.
///
/// Data dictionary.
/// Deserialized list.
private IList DeserializeData(JArray data)
{
var list = data.ToObject>();
for (int i = 0; i < list.Count; i++)
{
var value = list[i];
if (value is JObject)
list[i] = DeserializeData(value as JObject);
if (value is JArray)
list[i] = DeserializeData(value as JArray);
}
return list;
}