I have a complex JSON request that I need to output from GET (ie. www.mywebsite.com/controller/id) and this the JSON result I need to output (JSONfile)
{
"conf1": {
"default": "string1",
"default2": "string2"
},
"conf2": {
"intNr": "integer",
"intNr2": "integer",
"str1": "string1",
"emptyArray": []
},
"conf3": {
"infomain": {
"info1": "string",
"info2": "string",
"info3": "string",
"info4": "string"
},
"conf4": [
{
"str1": "string",
"str2": "string",
"str3": "string",
"str4": "string",
"str5": "string",
"str6": "string",
"str7": "string",
"str8": "string",
"str9": "string",
"str10": "string",
"str11": "string"
},
{
"str1": "string",
"str2": "string",
"str3": "string",
"str4": "string",
"str5": "string",
"str6": "string",
"str7": "string",
"str8": "string",
"str9": "string",
"str10": "string",
"str11": "string"
},
{
"str1": "string",
"str2": "string",
"str3": "string",
"str4": "string",
"str5": "string",
"str6": "string",
"str7": "string",
"str8": "string",
"str9": "string",
"str10": "string",
"str11": "string"
}
],
"conf5": {
"str1": "string",
"str2": "string",
"str3": "string"
},
"stringC": "string",
"stringN": "string",
"stringN2": "string",
"stringN3": "44",
"stringL": null,
"conf6": [
{
"strN": "string",
"strN2": "string"
},
{
"strN": "string",
"strN2": "string"
},
{
"strN": "string",
"strN2": "string"
},
{
"strN": "string",
"strN2": "string"
},
{
"strN": "string",
"strN2": "string"
}
]
},
"conf7": [
{
"strN": "string",
"strN2": "string",
"strN3": "string"
},
{
"strN": "string",
"strN2": "string",
"strN3": "string"
},
{
"strN": "string",
"strN2": "string",
"strN3": "string"
},
{
"strN": "string",
"strN2": "string",
"strN3": "string"
}
]
}
I am using Code First model and these are my models
namespace WEB.Models
{
[DataContract]
public class Jsonfile
{
public int ID { get; set; }
[DataMember(Name = "conf1")]
public conf1 conf1 { get; set; }
[DataMember(Name = "conf2")]
public conf2 conf2 { get; set; }
[DataMember(Name = "conf3")]
public conf3 conf3 { get; set; }
[DataMember(Name = "conf7")]
public ICollection<conf7> conf7s { get; set; }
//Foreign Keys
public int conf1Id { get; set; }
public int conf2Id { get; set; }
public int conf3Id { get; set; }
}
}
----------------------------------------------
namespace WEB.Models
{
[DataContract]
public class infomain
{
public int Id { get; set; }
[DataMember(Name = "info1")]
public string info1 { get; set; }
[DataMember(Name = "info2")]
public string info2 { get; set; }
[DataMember(Name = "info3")]
public string info3 { get; set; }
[DataMember(Name = "info4")]
public string info4 { get; set; }
}
}
-----------------------------------------
namespace WEB.Models
{
[DataContract]
public class conf1
{
public int Id { get; set; }
[DataMember(Name = "default")]
public string Default { get; set; }
[DataMember(Name = "default2")]
public string Default2 { get; set; }
}
}
------------------------------------
namespace WEB.Models
{
[DataContract]
public class conf2
{
public int Id { get; set; }
[DataMember(Name = "intNr")]
public string IntNr { get; set; }
[DataMember(Name = "intNr2")]
public string IntNr2 { get; set; }
[DataMember(Name = "str1")]
public string str1 { get; set; }
[DataMember(Name = "emptyArray")]
public string[] emptyArray { get; set; }
}
}
-----------------------------------
namespace WEB.Models
{
[DataContract]
public class conf3
{
public int Id { get; set; }
[DataMember(Name = "infomain")]
public infomain infomain { get; set; }
[DataMember(Name = "conf4")]
public IList<conf4> conf4s { get; set; }
[DataMember(Name = "conf5")]
public Conf5 Conf5 { get; set; }
[DataMember(Name = "stringC")]
public string stringC { get; set; }
[DataMember(Name = "stringN")]
public string stringN { get; set; }
[DataMember(Name = "stringN2")]
public string stringN2 { get; set; }
[DataMember(Name = "stringN3")]
public string stringN3 { get; set; }
[DataMember(Name = "stringL")]
public string stringL { get; set; }
[DataMember(Name = "conf6")]
public IList<conf6> conf6s { get; set; }
//Foreign keys
public int conf5Id { get; set; }
public int infomainId { get; set; }
}
}
-----------------------------------
namespace WEB.Models
{
[DataContract]
public class conf4
{
public string Id { get; set; }
[DataMember(Name = "str1")]
public string str1 { get; set; }
[DataMember(Name = "str2")]
public string str2 { get; set; }
[DataMember(Name = "str3")]
public string str3 { get; set; }
[DataMember(Name = "str4")]
public string str4 { get; set; }
[DataMember(Name = "str5")]
public string str5 { get; set; }
[DataMember(Name = "str6")]
public string str6 { get; set; }
[DataMember(Name = "str7")]
public string str7 { get; set; }
[DataMember(Name = "str8")]
public string str8 { get; set; }
[DataMember(Name = "str9")]
public string str9 { get; set; }
[DataMember(Name = "str10")]
public string str10 { get; set; }
[DataMember(Name = "str11")]
public string str11 { get; set; }
//Navigation
public conf3 conf3 { get; set; }
}
}
----------------------------
namespace WEB.Models
{
[DataContract]
public class conf5
{
public int Id { get; set; }
[DataMember(Name = "str1")]
public string str1 { get; set; }
[DataMember(Name = "str2")]
public string str2 { get; set; }
[DataMember(Name = "str3")]
public string str3 { get; set; }
}
}
--------------------------------
namespace WEB.Models
{
[DataContract]
public class conf6
{
public int ID { get; set; }
[DataMember(Name = "strN")]
public string strN { get; set; }
[DataMember(Name = "strN2")]
public string strN2 { get; set; }
}
}
-------------------------------
namespace WEB.Models
{
[DataContract]
public class conf7
{
public int Id { get; set; }
[DataMember(Name = "str1")]
public string str1 { get; set; }
[DataMember(Name = "str2")]
public string str2 { get; set; }
[DataMember(Name = "str3")]
public string str3 { get; set; }
//Navigation
public Jsonfile Jsonfile { get; set;}
}
}
-------------------------------------------
GET code from controller
// GET: api/Controller/ID
[ResponseType(typeof(JSONfile))]
public IHttpActionResult GetJSONFile(int id)
{
JSONfile JSONfile = db.JSONfiles.Find(id);
if (JSONfile == null)
{
return NotFound();
}
return Ok(JSONfile);
}
--------------------------
My problem is the data is null in my json result from these tables (conf1, conf2, conf3, conf7). Any suggestions or tips how to design and implement this API would be greatly appreciated. There is no requirements as far as project type or what I use as long as I can get JSONfile result formatted exactly and the data should be stored in database. I think I need to create a DTO class and seems to be a little too complex. This is the result I'm getting currently:
{
"conf1": null,
"conf2": null,
"conf3": null,
"conf7": null
}