我們可以使用C#中的字符串初始化JSON對象嗎?創建JSON對象或字符串..?
喜歡:"Person": [{"age":"42","name":"John"}]
作爲object JsonData = "Person": [{"age":"42","name":"John"}];
???
,這樣我可以直接給這個JSON對象到DatacontractJSONSerializer
,我可以得到的數據出來吧!
List<Person> people = new List<Person>{
new Person{age = 1, name = "Scott"},
new Person{age = 2, name = "Bill"}
};
string jsonString = ExtensionMethods.JSONHelper.ToJSON(people);
}
}
}
namespace ExtensionMethods
{
public static class JSONHelper
{
public static string ToJSON(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
public static string ToJSON(this object obj, int recursionDepth)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RecursionLimit = recursionDepth;
return serializer.Serialize(obj);
}
}
}
所以,
string jsonString = ExtensionMethods.JSONHelper.ToJSON(people);
給出的字符串:[{},{}]
空的數據結構,任何想法..?
你的問題可以改寫爲。 「我如何在C#中初始化一個JSON對象」 –
是的差不多...... !! – pvaju896