Answered by:
How to create json array string with Object

Question
-
User339833461 posted
Hello Everyone,
I have below code. how to write this object list into json array string using c#.
Thanks
List<Item> items = new List<Item> { new Item { name = "test1", index = "index1", itemDetails = new ItemProperties{ItemId = "39942dg", Price = 200.00 } }, new Item { name = "test2", index = "index2", itemDetails = new ItemProperties{ItemId = "67942dg", Price = 1000.00 } } }; public class Item { public string name { get; set; } public string index { get; set; } public ItemProperties itemDetails { get; set; } } public class RootObject { public List<Item> items { get; set; } } public class ItemProperties { public string ItemId { get; set; } public double Price { get; set; } }
Thanks
Thursday, April 23, 2020 12:57 PM
Answers
-
User503812343 posted
you can use NewtonSoft.Json like
private static void GetProductJson(DataTable table) { string JSONresult; JSONresult = JsonConvert.SerializeObject(table); Console.Write(JSONresult); }
check http://dotnetmentors.com/adonet/convert-datatable-to-csv-or-list-or-json-string.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 23, 2020 1:19 PM
All replies
-
User753101303 posted
Hi,
Do you mean that you expect a particular json output (an array of strings ???) or do you mean you just want the straightforward json for this ?
Also some context could help. ASP.NET handles serialization/deserialization for you so coding this explicitely should be done only when really needed. If writing a web service you could just return the object you want and ASP.NET will serialize this to json (or XML) for you...
Thursday, April 23, 2020 1:06 PM -
User339833461 posted
I need straightforward Json as a string :)
Thursday, April 23, 2020 1:08 PM -
User503812343 posted
you can use NewtonSoft.Json like
private static void GetProductJson(DataTable table) { string JSONresult; JSONresult = JsonConvert.SerializeObject(table); Console.Write(JSONresult); }
check http://dotnetmentors.com/adonet/convert-datatable-to-csv-or-list-or-json-string.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 23, 2020 1:19 PM -
User475983607 posted
I'm assuming you are using Newtonsoft. See the docs.
https://www.newtonsoft.com/json/help/html/SerializeObject.htm
https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
If this is an Web API question then just return the type and let the framework serialize the object. See the official web API docs.
Thursday, April 23, 2020 1:21 PM -
User753101303 posted
If this a web api it can even be :
public RootObject GetObject(int id) { // just return a .NET object }
Your code is called by ASP.NET based on the incoming url. ASP.NET then handle serializing the object returned by your code (it can even support using either json or XML based on what the incoming http request asked for).
It happens to see incorrect json being served sometimes without noticing this (ie the web api code serializes an object and ASP.NET serializes the string. it can get unoticed on the client side if the same error is done ie having a librarty deserializing the payload to a string and then having code deserializing the object.
And then when a 3rd party tries to use your APi it doesn't work for them because they correctly deserialize your josn payload one time only rather than mistakenly twice.
Thursday, April 23, 2020 6:28 PM