You must Sign In to post a response.
  • Category: .NET

    Convert Array to JSON in C#

    Hello.
    I want to convert array object into json.

    I have value,
    Array ( [order_no] => 1 )
    I want output as,
    {"order_no":"1"}

    How to do it.

    OR how to declare string variable with value, {"order_no":"1"} if conversion not happened.
    I want double quotes in it.
  • #767740
    Hai Pranjal,
    You can't convert array to JSON, you need to convert it to dictionary(Key & value) and then you can get the JSON.
    You can use the below code for the reference:

    string[] stringArr = new String[2];
    stringArr[0] = "order_no";
    stringArr[1] = "Order_Details";
    string jsonResults = JsonConvert.SerializeObject(stringArr );

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767794
    Hi,

    Use below sample


    var EmpName= "Pranjal";
    var Designation= "Software Engineer";

    var Emp= new
    {
    Name = EmpName,
    Desg= Designation
    };

    var json = JsonConvert.SerializeObject(Emp);

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments