我有一個JSON檔案,我正在從一個資料庫中提取,它的格式如下。
{
"n"/span>: [
{
"type": "standard.Rectangle"。
"id": "bc037b61-d4a5-4539-b3a9-3eb2183b61dd",
"x": 275,
"y": 205,
"text": "NBN HFC Modem
Gigafy NBN"。
"fill": "#666666",
"physicalLocation": "",
"thisInterfaceTypes": [
1
],
"otherInterfaceTypes":[
1
],
"link": [
"1a0757ff-9a6a-413c-ba5a-4461032c7b32"
],
"otherLocationEntityIds": [
null >。
],
"shouldUseLegacyLinkTypes":[
true
]
}
]
}
我現在需要從該JSON中獲取數值,并將其放入以下格式的JSON中。
{
"cell": [
{
"type": "app.Router"。
"size": {
"width": 90,
"高度": 35。
},
"position": {
"x": 170,
"y": 330。
},
"angle": 0,
"id": "987f80af-60be-4a2b-8844-d93a97fea85b",
"z": 1,
"attrs": {
".card": {
"fill": "透明"。
"stroke": "#F7B80A",
"stroke-dasharray": "0".
},
"image": {
"width": 42,
"高度": 42,
"ref": null。
"ref-x": null,
"ref-y": null,
"x": 16,
"y": 8,
"y-alignment": null,
"xlink:href": "assets/router2white.svg"。
},
".rank": {
"text-decoration": "無"。
"font-family": "Roboto Condensed",
"字體大小": 10,
"text": "Router",
"fill": "#f6f6f6",
"font-weight": "Bold"。
},
".name": {
"font-weight": "Normal",
"font-family": "Roboto Condensed",
"字體大小": 10,
"text": "IP Address",
"fill": "#f6f6f6"。
},
"root": {
"dataTooltipPosition": "left",
"dataTooltipPositionSelector": ".joint-stencil".
}
}
}
]
}
我目前正在使用newtonsoft json庫,對JSON進行反序列化和重新序列化,我目前可以改變屬性名稱和鍵值,但我的問題是,
我如何將'x'和'y'屬性嵌套到第二個JSON字串中的'position'屬性中?
我怎樣才能將'x'和'y'屬性嵌套到第二個JSON字串中所示的'position'屬性中。我目前可以做到這一點,但它總是將資料剝離并顯示為空。
這是我的類結構
。 public class N
{
public object type { get; set; }
public object entitiesId { get; set; }
public object x { get; set; }
public object y { get; set; }
public object text { get; set; }
public object color { get; set; }
public object physicalLocation { get; set; }
public object[] thisInterfaceTypes { get; set; }
public object[] otherInterfaceTypes { get; set; }
public object[] otherDeviceEntityIds { get; set; }
public object[] otherLocationEntityIds { get; set; }
public bool[] shouldUseLegacyLinkTypes { get; set; }
[JsonExtensionData]
public Dictionary<String, Object> AdditionalData { get; set; }
[JsonExtensionData]。
public Dictionary<String, Object> AdditionalData1 { get; set; }
[JsonExtensionData]
public Dictionary<String, Object> AdditionalData2 { get; set; }
任何幫助都將是巨大的,謝謝。
編輯:
更簡單地說,我需要把這個變成
{"x"/span>: 275, "y": 205}。
into
"position"/span>: {"x":170,"y":330}。
謝謝你
uj5u.com熱心網友回復:
如果你真的無法控制傳入資料的形狀,并且你真的需要傳出的資料是不同的形狀,那么你應該創建一個物件來反序列化傳入的資料,以及另一個物件來序列化傳出的資料。
在用于傳出資料的物件中,您可以創建一個建構式,接受傳入物件并根據需要建立傳出物件。
例如:
using System;
using Newtonsoft.Json;
public class Program public static void Main()
{
const string inJson = @"{"x": 275, ""y": 205}";
var deserialized = JsonConvert.DeserializeObject<InStructure>(inJson)。
var converted = new OutStructure(deserialized);
var reserialized = JsonConvert.SerializeObject(convertised);
Console.WriteLine(reserialized)。
}
}
public class InStructure{
public int x {get; set; }
public int y {get; set; }
}
public class OutStructure{
public Position Position{get;set; }
public OutStructure(InStructure inJson){
this.Position = new Position{
X = inJson.x,
Y = inJson.y
};
}
}
public class Position{
public int X {get; set; }
public int Y {get; set; }
輸出:
{"Position":{"X":275,"Y":205}}
見:
https://dotnetfiddle.net/jbvQjx
我把屬性名稱大寫,但你可以使用json屬性屬性把它們重命名為你想要的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322794.html
標籤:
