Reputation: 11388
I can't figure out, how to convert a Json input into a c# model.
Here is an example of the Json my controller gets :
[{"name":"StartDate",
"value":"0",
"Processors":[[{"processor":{"ProcessorName":"searchAndReplace","ProcessorParameters":[{"ParameterName":"toto"},{"ParameterName":"taa"}]}},
{"processor":{"ProcessorName":"searchAndReplace","ProcessorParameters":[{"ParameterName":"toto"},{"ParameterName":"tuuut"}]}}]]},
{"name":"EndDate","value":"1","Processors":[[]]},
{"name":"Country","value":"2","Processors":[[]]}]
Here is the signature of my controller :
public JsonResult SendMapping(List<Mapping> MyMappings)
This is my class "Mapping" :
public class Mapping
{
public string Value { get; set; }
public string Name { get; set; }
public List<Processor> Processors { get; set; }
}
And then my "ProcessorModel"
public enum ProcessorNames { SearchAndReplace }
public class ProcessorModel
{
public Processor processor;
}
public class Processor
{
public ProcessorNames ProcessorName;
public List<Parameter> ProcessorParameters;
}
public class Parameter
{
public List<string> ParameterName;
}
So far, I can get all the name and value. I can also retrieve the first ProcessorName, but not its parameters, nor other ProcessorNames
Upvotes: 1
Views: 1147
Reputation: 1150
It seems to me there is some issue related with lowercase and uppercase property names in your json. Property/Key name must be in same case as defined in class. Please correct that and use below class to Serialize or desalinize.
public class JsonHelper
{
/// <summary>
/// JSON Serialization
/// </summary>
public static string JsonSerializer<T>(T t)
{
var ser = new DataContractJsonSerializer(typeof(T));
var ms = new MemoryStream();
ser.WriteObject(ms, t);
string jsonString = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return jsonString;
}
/// <summary>
/// JSON Deserialization
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{
var ser = new DataContractJsonSerializer(typeof(T));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
}
Upvotes: 1
Reputation: 1298
I also don't think your JSON is quite right.
It should be
[{
"name": "StartDate",
"value": "0",
"Processors": [{
"processor": {"ProcessorName": "searchAndReplace",
"ProcessorParameters": [{
"ParameterName": "toto"
},
{
"ParameterName": "taa"
}]},
"processor": {"ProcessorName": "searchAndReplace",
"ProcessorParameters": [{
"ParameterName": "toto"
},
{
"ParameterName": "taa"
}]},
},
{
"processor": {"ProcessorName": "searchAndReplace",
"ProcessorParameters": [{
"ParameterName": "toto"
},
{
"ParameterName": "taa"
}]},
"processor": {"ProcessorName": "searchAndReplace",
"ProcessorParameters": [{
"ParameterName": "toto"
},
{
"ParameterName": "taa"
}]},
}]
}]
Upvotes: 1
Reputation: 1057
Just a useful tool when you struggle with JSON to Class conversion.
This is very useful to win time. Because I have spend a lot of unnecessary time on debugging JSON de-serialization in the past.
Upvotes: 4
Reputation: 4445
{"ParameterName":"toto"}
is defined in your json (string) as a string and in the POCO you have it as public List<string> ParameterName;
the json string should be {"ParameterName":["toto"]}
Upvotes: 1
Reputation: 1298
I don't think you need the ProcessorModel.
You're mapping class should just contain:
public List<Processor> Processors { get; set; }
instead of
public List<ProcessorModel> Processors { get; set; }
Upvotes: 1