我有两个DataTable,RCPT_HEADER和RCPT_DETAIL,并尝试使用Json.NET / C#序列化为json对象层次结构。 我已经尝试了代码
static JArray DataToArray(string connString, string query)
{
JArray jArray = new JArray();
try
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
int fieldcount = reader.FieldCount;
object[] values = new object[fieldcount];
reader.GetValues(values);
JObject jo = new JObject();
for (int index = 0; index < fieldcount; index++)
{
jo.Add(reader.GetName(index).ToString(), values[index].ToString());
}
jArray.Add(jo);
}
reader.Close();
}
}
}
catch (SqlException e)
{
WriteLog("[DataToArray]: " + e.Message);
}
return jArray;
}
和
static void Main(string[] args)
{
try
{
Hashtable config = getSettings(AppPath() + "mware.config");
string connString = config["cs"].ToString();
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter jsonWriter = new JsonTextWriter(sw);
jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
JObject jObject = new JObject();
JArray jArray = new JArray();
jObject.Add("RCPT_HEADER", DataToArray(connString, "SELECT * FROM RCPT_HEADER"));
jObject.Add("RCPT_DETAIL", DataToArray(connString, "SELECT * FROM RCPT_DETAIL"));
jObject.WriteTo(jsonWriter);
Console.WriteLine(jObject.ToString());
Console.ReadLine();
}
catch (Exception e)
{
WriteLog("[postJSON]: " + e.Message);
}
}
但是我这样输出:
{
"RCPT_HEADER": [
{
"RECORD_ID": "1",
"ACTION_CODE": "SAVE",
"CONDITION": "Ready",
"DATE_TIME_STAMP": "9/11/2015 12:00:00 AM"
}
],
"RCPT_DETAIL": [
{
"RECORD_ID": "1",
"ACTION_CODE": "SAVE",
"CONDITION": "Ready",
"LINK_ID": "1",
"ITEM": "SKU00048700007683",
"DATE_TIME_STAMP": "9/11/2015 12:00:00 AM"
},
{
"RECORD_ID": "2",
"ACTION_CODE": "SAVE",
"CONDITION": "Ready",
"LINK_ID": "1",
"ITEM": "SKU00048700007684",
"DATE_TIME_STAMP": "9/11/2015 12:00:00 AM"
}
]
}
实际上,我希望它能像这样返回输出:
{
"RCPT_HEADER": [
{
"RECORD_ID": "1",
"ACTION_CODE": "SAVE",
"CONDITION": "Ready",
"DATE_TIME_STAMP": "9/11/2015 12:00:00 AM",
"RCPT_DETAIL": [
{
"RECORD_ID": "1",
"ACTION_CODE": "SAVE",
"CONDITION": "Ready",
"LINK_ID": "1",
"ITEM": "SKU00048700007683",
"DATE_TIME_STAMP": "9/11/2015 12:00:00 AM"
},
{
"RECORD_ID": "2",
"ACTION_CODE": "SAVE",
"CONDITION": "Ready",
"LINK_ID": "1",
"ITEM": "SKU00048700007684",
"DATE_TIME_STAMP": "9/11/2015 12:00:00 AM"
}
]
}
]
}
任何帮助都会非常感激,因为这是我第一次尝试使用JSON.NET。
答案 0 :(得分:0)
当您使用数据表时,它将以这种方式将表序列化为JSON数组表
您可以编写自己的JsonConverter
,以便按照自己的方式对其进行序列化。这是a good article。
但是,在我看来,还有另一个好的但是hacky解决方案:如果你总是拥有相同的结构并且不需要太多的性能,那么再次反序列化它会更容易,移动这个对象并将其序列化。
像这样:
string json = "YOUR_JSON_RESULT";
JObject jsonObject = JObject.Parse(json);
((JObject)jsonObject["RCPT_HEADER"][0])
.Properties()
.Last()
.AddAfterSelf(new JProperty("RCPT_DETAIL", jsonObject["RCPT_DETAIL"]));
jsonObject.Remove("RCPT_DETAIL");
string jsonResult = jsonObject.ToString();
还有另一种优雅且听起来合适的解决方案。您使用数据表,但是您需要通过实现自定义序列化程序或修改器来使其变为序列化,就像它不是表格一样。可能你只是不需要数据表 如果可能,请不要使用它 - 使用您自己的类,以便您可以描述任何嵌套和序列化顺序:
public class RcptDetail
{
public string RECORD_ID { get; set; }
/* ... */
}
public class RcptHeader
{
public string RECORD_ID { get; set; }
/* ... */
public RcptDetail RCPT_DETAIL { get; set; }
}