使用c#将Json响应转换为字典(键值对)



我有这样的东西

{
"WellContactInfo": {
"Customer": "Wintershall",
"RigName": "Deepsea Aberdeen",
"WellId": null
}
}

我想把它转换成这样

{
"WellContactInfo": {
{
"name" : "Customer",
"value" : "Wintershall"
},
{
"name": "RigName",
"value" : "Deepsea Aberdeen"
},
{
"name": "WellId",
"value" : null
}
}
}

我有一个名为WellContactInfo的模型,并将值分配给其中的属性。请建议我一个方法如何实现同样的。如果您还需要这方面的信息,请告诉我。

注意:上面的Json是一个示例,我有Json中有多个类。

{
public int Id { get; set; }
public string Customer { get; set; } 
public string RigName { get; set; }
public string WellId { get; set; } 
//more entries
}
public class DownholeLoggingData
{
public DownholeLoggingData()
{
MWDCurvesDetails = new List<MWDCurvesDetails>();
HoleSizeMap = new HoleSizeMap();
}
public int Id { get; set; } = 0;
public bool CompressedTelemetry { get; set; } = false;
public bool LegacyTelemetry { get; set; } = false;
public HoleSizeMap HoleSizeMap { get; set; }
public List<MWDCurvesDetails> MWDCurvesDetails { get; set; }
}
public class SurfaceLoggingData
{
public SurfaceLoggingData()
{
SDLCurvesDetails = new List<SDLCurvesDetails>();
HoleSizeMap = new HoleSizeMap();
}
public int Id { get; set; } = 0;
public HoleSizeMap HoleSizeMap { get; set; }
public List<SDLCurvesDetails> SDLCurvesDetails { get; set; }
}
{
public int Id { get; set; } 
public string Holesize1 { get; set; }
public string Holesize2 { get; set; }
public string Holesize3 { get; set; }
public string Holesize4 { get; set; }
public string Holesize5 { get; set; }
public string Holesize6 { get; set; }
public string Holesize7 { get; set; }
public string Holesize8 { get; set; }
//more properties
}
public class SDLCurvesDetails
{
public int Id { get; set; } 
public string Holesize1 { get; set; }
public string Holesize2 { get; set; }
public string Holesize3 { get; set; }
public string Holesize4 { get; set; }
public string Holesize5 { get; set; }
public string Holesize6 { get; set; }
public string Holesize7 { get; set; }
public string Holesize8 { get; set; }
//more properties
}

我知道这越来越复杂了。但我有像这样的单独模型。并希望将所有内容转换为上述格式。

注意:更新了问题并添加了更多的类。

当前完整的Json响应在这里

try this

var jsonParsed = JObject.Parse(json);
var wellContactInfo = new Data
{
WellContactInfo = ((JObject)jsonParsed["WellContactInfo"]).Properties()
.Select(v => new KeyValuePair<string, string>(v.Name, (string)v.Value)).ToList(),

DownholeLoggingData = new DownholeLoggingData
{
Id = (int)jsonParsed["DownholeLoggingData"]["Id"],
CompressedTelemetry = (bool)jsonParsed["DownholeLoggingData"]["CompressedTelemetry"],
LegacyTelemetry = (bool)jsonParsed["DownholeLoggingData"]["LegacyTelemetry"],
HoleSizeMap = ((JObject)jsonParsed["DownholeLoggingData"]["HoleSizeMap"])
.ToObject<Dictionary<string, string>>(),
MWDCurvesDetails = ((JArray)jsonParsed["DownholeLoggingData"]["MWDCurvesDetails"])
.Select(m => ((JObject)m).Properties().Select(v => new KeyValuePair<string, string>(v.Name, (string)v.Value)).ToList()).ToList()
}
};

public class Data
{
public List<KeyValuePair<string, string>> WellContactInfo { get; set; }
public DownholeLoggingData DownholeLoggingData { get; set; }
}
public class DownholeLoggingData
{
public int Id { get; set; }
public bool CompressedTelemetry { get; set; }
public bool LegacyTelemetry { get; set; }
public Dictionary<string, string> HoleSizeMap { get; set; }
public List<List<KeyValuePair<string, string>>> MWDCurvesDetails { get; set; }
}

但是我认为最好是创建MWDCurvesDetail并定义MWDCurvesDetails如下

public List< MWDCurvesDetail> MWDCurvesDetails { get; set; }

非常感谢@Serge的帮助。我对代码做了一些更多的更改,并能够得到预期的结果。

var json = JsonConvert.SerializeObject(mainWorkOrderData);
var jsonParsed = JObject.Parse(json);

var finalData = new FinalData
{
WellContactInfo = ((JObject)jsonParsed["WellContactInfo"]).Properties()
.Select(v => new KeyValuePair<string, dynamic>(v.Name, (string)v.Value)).ToList(),
DownholeLoggingData = new DownholeLoggingDataDictionary
{
Id = new KeyValuePair<string, int>("Id", (int)jsonParsed["DownholeLoggingData"]["Id"]),
CompressedTelemetry = new KeyValuePair<string, bool>("CompressedTelemetry", (bool)jsonParsed["DownholeLoggingData"]["CompressedTelemetry"]),
LegacyTelemetry = new KeyValuePair<string, bool>("LegacyTelemetry", (bool)jsonParsed["DownholeLoggingData"]["LegacyTelemetry"]),
HoleSizeMap = ((JObject)jsonParsed["DownholeLoggingData"]["HoleSizeMap"])
.Properties().Select(v => new KeyValuePair<string, string>(v.Name, (string)v.Value)).ToList(),
MWDCurvesDetails = ((JArray)jsonParsed["DownholeLoggingData"]["MWDCurvesDetails"])
.Select(m => ((JObject)m).Properties().Select(v => new KeyValuePair<string, dynamic>(v.Name, v.Value)).ToList()).ToList()
},
SurfaceLoggingData = new SurfaceLoggingDataDictionary
{
Id = new KeyValuePair<string, int>("Id", (int)jsonParsed["SurfaceLoggingData"]["Id"]),
HoleSizeMap = ((JObject)jsonParsed["SurfaceLoggingData"]["HoleSizeMap"])
.Properties().Select(v => new KeyValuePair<string, string>(v.Name, (string)v.Value)).ToList(),
SDLCurvesDetails = ((JArray)jsonParsed["SurfaceLoggingData"]["SDLCurvesDetails"])
.Select(m => ((JObject)m).Properties().Select(v => new KeyValuePair<string, dynamic>(v.Name, v.Value)).ToList()).ToList()
}
};
public class DownholeLoggingData
{
public DownholeLoggingData()
{
MWDCurvesDetails = new List<MWDCurvesDetails>();
HoleSizeMap = new HoleSizeMap();
}
public int Id { get; set; } = 0;
public bool CompressedTelemetry { get; set; } = false;
public bool LegacyTelemetry { get; set; } = false;
public HoleSizeMap HoleSizeMap { get; set; }
public List<MWDCurvesDetails> MWDCurvesDetails { get; set; }
}
public class DownholeLoggingDataDictionary
{
public KeyValuePair<string, int> Id { get; set; }
public KeyValuePair<string, bool> CompressedTelemetry { get; set; }
public KeyValuePair<string, bool> LegacyTelemetry { get; set; }
public List<KeyValuePair<string, string>> HoleSizeMap { get; set; }
public List<List<KeyValuePair<string, dynamic>>> MWDCurvesDetails { get; set; }
}
public class SurfaceLoggingData
{
public SurfaceLoggingData()
{
SDLCurvesDetails = new List<SDLCurvesDetails>();
HoleSizeMap = new HoleSizeMap();
}
public int Id { get; set; } = 0;
public HoleSizeMap HoleSizeMap { get; set; }
public List<SDLCurvesDetails> SDLCurvesDetails { get; set; }
}

在所有这些更改之后,我得到以下响应作为Json

public class SurfaceLoggingDataDictionary
{
public KeyValuePair<string, int> Id { get; set; }
public List<KeyValuePair<string, string>> HoleSizeMap { get; set; }
public List<List<KeyValuePair<string, dynamic>>> SDLCurvesDetails { get; set; }
}

在做了所有这些更改之后,我得到了预期的响应。

最新更新