Reputation: 1084
Hi My json structure is like this
{
"OutputParam": [
{
"param1": "John",
"param2": "Doe",
"param3": "BT",
"param4": [
{
"subparam1": "00002",
"subparam2": "True",
"subparam3": "-",
"subparam4": "-",
"subparam5": "data goes here"
},
{
"subparam1": "00003",
"subparam2": "True",
"subparam3": "-",
"subparam4": "-",
"subparam5": "data goes here"
},
{
"subparam1": "00004",
"subparam2": "False",
"subparam3": "111",
"subparam4": "message",
"subparam5": "-"
}
]
}
]
}
and the classes which I get from http://json2csharp.com/ is as follows
public class Param4
{
public string subparam1 { get; set; }
public string subparam2 { get; set; }
public string subparam3 { get; set; }
public string subparam4 { get; set; }
public string subparam5 { get; set; }
}
public class OutputParam
{
public string param1 { get; set; }
public string param2 { get; set; }
public string param3 { get; set; }
public List<Param4> param4 { get; set; }
}
public class RootObject
{
public List<OutputParam> OutputParam { get; set; }
}
now the issues is I want to pass above Json as API output in c# API, I tried list and var but so far not able to return the above mentioned Json, can any one help or at least give a direction?
Upvotes: 1
Views: 77
Reputation: 131
Here is small sample of code that can help you
public RootObject1 Get(int id)
{
RootObject1 rt = new RootObject1();
OutputParam pr = new OutputParam();
Param4 cr = new Param4();
rt.OutputParam = new List<OutputParam>();
pr.Param4= new List<Param4>();
pr.Param1= "AB";
rt.OutputParam = new List<OutputParam>();
cr.Param6 = "aceee";
pr.Param4.Add(cr);
rt.OutputParam.Add(pr);
return rt;
}
Upvotes: 1
Reputation: 642
your webservice c# prolly return an xml whith your json as string in? this is the default
so you have different choice
go for xml or add this line to your methode
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
Upvotes: 0
Reputation: 6425
If you want to return as per your example using Web API a method such as
public RootObject GetRootObject()
{
return new RootObject();
}
Would do it, assuming the request to the API asked for JSON in the content negotiation - the web api will derserialise your object into JSON auto-magically
Upvotes: 1