在 C# 中动态创建 Json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10252675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Json dynamically in c#
提问by Alaa Osta
I need to create a Json object dynamically by looping through columns. so declaring an empty json object then add elements to it dynamically.
我需要通过循环列来动态创建一个 Json 对象。所以声明一个空的 json 对象然后动态地向它添加元素。
eg:
例如:
List<String> columns = new List<String>{"FirstName","LastName"};
var jsonObj = new {};
for(Int32 i=0;i<columns.Count();i++)
jsonObj[col[i]]="Json" + i;
And the final json object should be like this:
最终的 json 对象应该是这样的:
jsonObj={FirstName="Json0", LastName="Json1"};
采纳答案by David Peden
[TestFixture]
public class DynamicJson
{
[Test]
public void Test()
{
dynamic flexible = new ExpandoObject();
flexible.Int = 3;
flexible.String = "hi";
var dictionary = (IDictionary<string, object>)flexible;
dictionary.Add("Bool", false);
var serialized = JsonConvert.SerializeObject(dictionary); // {"Int":3,"String":"hi","Bool":false}
}
}
回答by mattytommo
You should use the JavaScriptSerializer. That can Serialize actual types for you into JSON :)
您应该使用JavaScriptSerializer. 这可以为您将实际类型序列化为 JSON :)
Reference: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
参考:http: //msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
EDIT: Something like this?
编辑:像这样的东西?
var columns = new Dictionary<string, string>
{
{ "FirstName", "Mathew"},
{ "Surname", "Thompson"},
{ "Gender", "Male"},
{ "SerializeMe", "GoOnThen"}
};
var jsSerializer = new JavaScriptSerializer();
var serialized = jsSerializer.Serialize(columns);
Output:
输出:
{"FirstName":"Mathew","Surname":"Thompson","Gender":"Male","SerializeMe":"GoOnThen"}
回答by ghiscoding
I found a solution very similar to DPeden, though there is no need to use the IDictionary, you can pass directly from an ExpandoObjectto a JSON convert:
我找到了一个和 DPeden 非常相似的解决方案,虽然不需要使用 IDictionary,但您可以直接从 an 传递ExpandoObject到 JSON 转换:
dynamic foo = new ExpandoObject();
foo.Bar = "something";
foo.Test = true;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);
and the output becomes:
输出变为:
{"Bar":"something","Test":true}
回答by Andrew
Using dynamicand JObject
使用dynamic和JObject
dynamic product = new JObject();
product.ProductName = "Elbow Grease";
product.Enabled = true;
product.StockCount = 9000;
Console.WriteLine(product.ToString());
// {
// "ProductName": "Elbow Grease",
// "Enabled": true,
// "StockCount": 9000
// }
Or how about:
或者怎么样:
JObject obj = JObject.FromObject(new
{
ProductName = "Elbow Grease",
Enabled = true,
StockCount = 9000
});
Console.WriteLine(obj.ToString());
// {
// "ProductName": "Elbow Grease",
// "Enabled": true,
// "StockCount": 9000
// }
https://www.newtonsoft.com/json/help/html/CreateJsonDynamic.htm
https://www.newtonsoft.com/json/help/html/CreateJsonDynamic.htm

