1

I have a following JSON String , which is passed as a input parameter of my Web-API. I face trouble in accessing the multilevel JSON data , only NULL value is receiving in parameter.

{"Customer":{"Abc":67,"Def":"main_user","Hij":"0123","Kel":0},"CustomerOrder":{"OrderID":1,"CartId":1,"Amount":10.00,"LogId":123,"UserId":4},"Actions": [      "value": "New", "onclick": "CreateNewDoc()"},      {"value": "Open", "onclick": OpenDoc()"},      {"value": "Close", "onclick": "CloseDoc()"}    ]}

Class In C#

 public class Rootobject
{
    public Customer Customer { get; set; }
    public Customerorder CustomerOrder { get; set; }
    public Action[] Actions { get; set; }
}

public class Customer
{
    public int Abc { get; set; }
    public string Def { get; set; }
    public string Hij { get; set; }
    public int Kel { get; set; }
}

public class Customerorder
{
    public int OrderID { get; set; }
    public int CartId { get; set; }
    public float Amount { get; set; }
    public int LogId { get; set; }
    public int UserId { get; set; }
}

public class Action
{
    public string value { get; set; }
    public string onclick { get; set; }
}

Here i accept the data from postbody,

[Route("~/FetchData")]
 //   [ActionName("VoucherStatus")]
    [HttpPost]
    //  GET: http://localhost:28056/FetchData/ // 

    public ProcessedResponse<DashBoradController> ProcessVoucherFetch([FromBody] Rootobject  request)
    { // Some operatons } 

Is it possible to read entire JSON string like above ? Can any one help to fix, i have did the one layer of JSON like this below,

{ "Abc":67, "Def":"main_user", "Hij":"0123", "Kel":0 }

But multilevel JSON gives NULL.

jidh
  • 172
  • 1
  • 6
  • 22

1 Answers1

1

Your JSON is not valid. Here is valid JSON that should bind to your object:

{
  "Customer": {
    "Abc": 67,
    "Def": "main_user",
    "Hij": "0123",
    "Kel": 0
  },
  "CustomerOrder": {
    "OrderID": 1,
    "CartId": 1,
    "Amount": 10,
    "LogId": 123,
    "UserId": 4
  },
  "Actions": [
    {
      "value": "New",
      "onclick": "CreateNewDoc()"
    },
    {
      "value": "Open",
      "onclick": "OpenDoc()"
    },
    {
      "value": "Close",
      "onclick": "CloseDoc()"
    }
  ]
}
Luke
  • 22,826
  • 31
  • 110
  • 193
  • Dear Luke, Thank you for the correction, but still i am getting NULL response in parameter list. – jidh Apr 22 '17 at 08:53
  • Hello. How are you posting your JSON to your controller, please can you share the code that is doing this? – Luke Apr 22 '17 at 08:54
  • I have shared it above, can you check it please . – jidh Apr 22 '17 at 08:57
  • I can't see anything that is performing the POST to your action method...? – Luke Apr 22 '17 at 08:59
  • 2
    I don't think you're understanding me. The Action method is fine, but we need to see where the request is coming from in order to diagnose why the message received isn't being bound to your model... – Luke Apr 22 '17 at 09:49