Trending Tutorials

C# tips and tricks 7 – Converting JSON data to C# Object (jsonplaceholder.typicode and json2csharp)

Converting JSON data to C# Object:
Newtonsoft.Json :
It is a popular high-performance JSON framework for .NET

Downloads the requested resource as a String. The resource to download is specified as a String containing the URL.

Serialization :
Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an…

17 Comments on C# tips and tricks 7 – Converting JSON data to C# Object (jsonplaceholder.typicode and json2csharp)

  1. Nice and simple. I spent several hours looking for the equivalent before finding this video. Thanks!

  2. Can you please show example of multi level classes like
    public class Internalid


    public string value { get; set; }

    public string text { get; set; }


    public class Values


    public List<Internalid> internalid { get; set; }

    public string itemid { get; set; }

    public string displayname { get; set; }


    public class RootObject


    public string recordType { get; set; }

    public string id { get; set; }

    public Values values { get; set; }


  3. I'm receiving a Stream and if I just parse to string the json is fine, but when I pass to DeserializeObject the data is returning null and 0. What could be wrong?

  4. Perfect. Works 100% Thank you for the video

  5. The class should be called Post.cs, not Posts.cs – you class represents a single object.

  6. Edge WordPress Theme – Build an amazing site design without coding. Use our awesome drag & drop page builder and the fastest theme on the market ???????????????? 
    Build better websites with Visualmodo and grow your business!

  7. Can you please show example of a very complex 5 levels down JSON object deserialisation

  8. This has terrified me for so long… AND IT'S SO SIMPLE! Thank you for explaining it so clearly.

  9. Thanks a lot.
    Deeply appreciative.

  10. Just what I was looking for. Now, what if I wanted to make a class list out of the JSON string. What would that look like?

  11. Nice bro keep it up thanks for this tutorial ????

  12. I've been looking for a video like this for almost 2h by now. Thanks very much!

  13. I need to deserialize multiple objects, one I can but I am looking desperately for more objects to deserialize

  14. hola gracias por el video. tengo el siguiente error en visual 2017. en la linea

    Post post1 = JsonConvert.DeserializeObject<Post>(text);

    me salio el siguiente error:
    Se produjo la excepción Newtonsoft.Json.JsonSerializationException.
    Mensaje = Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'json2.Post' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
    To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
    Path '', line 4, position 1.
    Origen = Newtonsoft.Json
    Seguimiento de la pila:
    en Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)
    en Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
    en Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
    en Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
    en Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
    en Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
    en Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
    en Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
    en json2.Form1.Form1_Load(Object sender, EventArgs e) en D:AYUDASc#EJEMPLOSJSON-DECODIFICAjson-decodifica2json2json2Form1.cs: línea 30
    en System.Windows.Forms.Form.OnLoad(EventArgs e)
    en System.Windows.Forms.Form.OnCreateControl()
    en System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    en System.Windows.Forms.Control.CreateControl()
    en System.Windows.Forms.Control.WmShowWindow(Message& m)
    en System.Windows.Forms.Control.WndProc(Message& m)
    en System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    en System.Windows.Forms.Form.WmShowWindow(Message& m)
    en System.Windows.Forms.Form.WndProc(Message& m)
    en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    alguna idea del porque?

  15. perfect, exactly what I was looking for. There you go, very pleasant to have you on my screen. Thanks a lot paw.

Leave a comment

Your email address will not be published.