I am looking for a way to get this
{
"parameters": {
"object1": {
"propertie1": "value",
"propertie2": "value"
},
"object2": {
"propertie1": "value",
"propertie2": "value"
}
}
}
C# i got an object like this
public class MyObject
{
public Dictionnary<string, List < KeyValuePai < string, object > > > parameters = new Dictionnary<string, List < KeyValuePai < string, object > > >();
}
I add data like this in this dictionnary :
MyObject.AddParameters("object1", new List < KeyValuePair < string, object > > {
new KeyValuePair < String, object >("propertie1", "value"),
new KeyValuePair < String, object >("propertie2", "value"),
}
MyObject.AddParameters("object2", new List < KeyValuePair < string, object > > {
new KeyValuePair < String, object >("propertie1", "value"),
new KeyValuePair < String, object >("propertie2", "value"),
}
Put when i decode my serialized object in php i got :
{
"parameters": {
"object1": [
{
"Key": "propertie1",
"Value": "value"
},
{
"Key": "propertie2",
"Value": "value"
}
],
"object2": [
{
"Key": "propertie1",
"Value": "value"
},
{
"Key": "propertie2",
"Value": "value"
}
]
},
}
How can i get the correct format from the JSON.Net serialization?
You'll need to add the data in C# in the form that you want.
The way you add data now, will indeed convert to that output.
You'll need something like this in C#:
MyObject.AddParameters("object1", new List < KeyValuePair < string, object > > {
new KeyValuePair < String, object >("propertie1", "value"),
new KeyValuePair < String, object >("propertie2", "value"),
}
MyObject.AddParameters("object2", new List < KeyValuePair < string, object > > {
new KeyValuePair < String, object >("propertie1", "value"),
new KeyValuePair < String, object >("propertie2", "value"),
}
Edit:
A better way is to let the JsonConvert.Serialize do his job but insted of set a List<KeyValuePair>
as parameter i pass an object.
Code is now
public class MyObject
{
public Dictionnary<string, object> parameters = new Dictionnary<string,object>();
}
And adding data to paremeters like this
MyObject.AddParameters("object1", new {
propertie1 ="value",
propertie2 = "value",
});
MyObject.AddParameters("object2", new {
propertie1 ="value",
propertie2 = "value",
});
Now the serialized Dictionnary as the format expected.
Original answer :
Got it with 21021655 question.
Need to write my own Converter like said in answer
class MyConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
List<KeyValuePair<string, object>> list = value as List<KeyValuePair<string, object>>;
writer.WriteStartArray();
foreach (var item in list)
{
writer.WriteStartObject();
writer.WritePropertyName(item.Key);
writer.WriteValue(item.Value);
writer.WriteEndObject();
}
writer.WriteEndArray();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// TODO...
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<KeyValuePair<string, object>>);
}
}
And return this result [{"one":1},{"two":2},{"three":3}]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.