[英]How to serialize C# objects to generate specific JSON
I am trying to create JSON like the following to pass to an external via TCP. 我正在尝试创建如下所示的JSON,以通过TCP传递给外部。
{"method": "dither", "params": [10, false, {"pixels": 1.5, "time": 8, "timeout": 40}], "id": 42}
{“方法”:“抖动”,“参数”:[10,否,{“像素”:1.5,“时间”:8,“超时”:40}],“标识”:42}
I came close, but this is what I got instead: 我接近了,但这是我得到的:
{"method": "dither", "params": [10, false, " {"pixels": 1.5, "time": 8, "timeout": 40} " ], "id": 42}
{“方法”:“抖动”,“参数”:[10,否, “ {”像素”:1.5,“时间”:8,“超时”:40} “ ],“ id”:42}
Notice the quote marks around the 3rd element of the params array. 注意params数组的第3个元素周围的引号。
I would appreciate any help in resolving this. 对于解决此问题,我将不胜感激。 Here is my code:
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Collections;
using System.Xml;
using System.Reflection;
namespace DitherTest
{
[CollectionDataContract]
public class DitherParametersList : ArrayList
{
public DitherParametersList() : base()
{}
}
[DataContract]
public class Dither
{
[DataMember( Name="method", Order=1)]
public string Method { get; set; }
[DataMember( Name="params", Order=2)]
public DitherParametersList Parameters { get; set; }
[DataMember( Name="id", Order=3)]
public int Id { get; set; }
}
[DataContract( Namespace="")]
public class Settle
{
[DataMember( Name = "pixels" )]
public double Pixels { get; set; }
[DataMember( Name = "time" )]
public int Time { get; set; }
[DataMember( Name = "timeout" )]
public int Timeout { get; set; }
public string SerializeJson()
{
return this.ToJSON();
}
}
static class Extensions
{
public static string ToJSON<T>( this T obj ) where T : class
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer( typeof( T ) );
using ( MemoryStream stream = new MemoryStream() )
{
serializer.WriteObject( stream, obj );
return Encoding.Default.GetString( stream.ToArray() );
}
}
}
class Program
{
static void Main( string[] args )
{
double ditherAmount = 10.0;
bool ditherRaOnly = false;
Settle settle = new Settle { Pixels = 1.5, Time = 8, Timeout = 40 };
DitherParametersList parameterList = new DitherParametersList();
parameterList.Add( ditherAmount );
parameterList.Add( ditherRaOnly );
string settleStr = settle.SerializeJson();
parameterList.Add( settleStr );
Dither dither = new Dither { Method = "dither", Parameters = parameterList, Id=42 };
string temp = dither.ToJSON();
}
}
}
Thanks in advance 提前致谢
you told it to make the third arg a string. 您告诉它使第三个arg成为字符串。 You serialized it to a string then stuck it in as an arg.
您将其序列化为字符串,然后将其作为arg插入。
you need 你需要
parameterList.Add( settle );
First of all, be sure to use newtonsoft.com/json like pm100 mentioned in his comment. 首先,请确保使用newtonsoft.com/json,如他的评论中提到的pm100 。 I've changed your code so that it would work with newtonsoft.json and got exactly what you've asked for:
我已经更改了您的代码,使其可以与newtonsoft.json一起使用,并且完全满足您的要求:
{"method": "dither", "params": [10, false, {"pixels": 1.5, "time": 8, "timeout": 40}], "id": 42}
I removed the DitherParametersList you've created and used those models: 我删除了您创建的DitherParametersList并使用了这些模型:
public class Dither
{
[JsonProperty("method", Order = 1)]
public string Method { get; set; }
[JsonProperty("params", Order = 2)]
public ArrayList Parameters { get; set; }
[JsonProperty("id", Order = 3)]
public int Id { get; set; }
}
public class Settle
{
[JsonProperty("pixels")]
public double Pixels { get; set; }
[JsonProperty("time")]
public int Time { get; set; }
[JsonProperty("timeout")]
public int Timeout { get; set; }
}
And serialized them easily: 并轻松序列化它们:
class Program
{
static void Main(string[] args)
{
var settle = new Settle { Pixels = 1.5, Time = 8, Timeout = 40 };
var parameterList = new ArrayList { 10, false, settle };
var dither = new Dither { Method = "dither", Parameters = parameterList, Id = 42 };
string temp = JsonConvert.SerializeObject(dither);
}
}
The quotes are coming from the first serialization that you did here: 引号来自您在此处进行的第一个序列化:
string settleStr = settle.SerializeJson();
Assuming that you want to avoid using the Newtonsoft library, an immediate fix would be to simply trim them off: 假设您要避免使用Newtonsoft库,一个直接的解决方法是简单地将它们修剪掉:
string settleStr = settle.SerializeJson().Trim('"');
A more robust solution would only require serialization one time. 一个更强大的解决方案将只需要序列化一次。 If you used a
List{string}
instead of a DitherParamtersList
, you could do this: 如果您使用
List{string}
而不是DitherParamtersList
,则可以执行以下操作:
Settle settle = new Settle { Pixels = 1.5, Time = 8, Timeout = 40 };
var parameterList = new List<string>()
{
ditherAmount.ToString(),
ditherRaOnly.ToString(),
string.Join(",", settle.Pixels, settle.Time, settle.Timeout)
};
Dither dither = new Dither { Method = "dither", Parameters = parameterList, Id = 42 };
string temp = dither.ToJSON();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.