Deserializing Json Object of objects in C#

deserialize json to object c#
deserialize json c#
deserialize json array c#
deserialize string to object c#
json.deserialize apex
newtonsoft json deserialize dynamic
convert string to json c# newtonsoft
deserialize json java

I've been struggling to get this to work for a while now... So, let's suppose I've got this .json file:

{
    "fieldConfs" : 
        {
            "Object1" : {"origin" : "IdArticulo"    , "destinationTable" : "Articulos", "destinationField" : "IdArticulo", "dataType" : "nvarchar"},
            "Object2" : {"origin" : "Nombre"    , "destinationTable" : "Articulos", "destinationField" : "Descrip", "dataType" : "nvarchar"}
        }
}

And I want to deserialize it into a list of Objects, so each Object inside "fieldConfs" (Object1, object2) will go into a List.

My code so far is:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace modules.dataProcessing.ConfManager
{
    public class Field
    {
        public string Origin { get; set; }

        public string DestinationTable { get; set; }

        public string DestinationField { get; set; }

        public string DataType { get; set; }

    }

    public class FieldConfs
    {
        public string Id
        {
            get; set;
        }

        public IList<Field> Fields
        {
            get; set;
        }
    }

    public static class JsonTools
    {
        public static List<FieldConfs> loadConf(string json)
        {
            List<FieldConfs> fC = new List<FieldConfs>();
            fC = JsonConvert.DeserializeObject<List<FieldConfs>>(json);
            return fC;
        }
    }
}

And I call it like this:

List<FieldConfs> confs = new List<FieldConfs>();
confs = JsonTools.loadConf(json);

I know I'm lacking something, but I'm new to C# and to Json as well and I don't know what it is that I'm missing. Any hint or help would be appreciate it. Thank you all beforehand.

EDIT: For a little more clarification, what I'm really asking is for advice on how should I do this, since I'm new.

I'd like to be able to deserialize the json into a list so I can later do in code:

foreach (List<FieldConfs> list in confs) {

     // Code for each list where list would be Object1... Object2... 
}

So maybe I'm not structuring well my Json... I don't know if I'm explaining myself well...

Copy the json into the paste buffer. open a new class in the editor, select 'Paste Special' from the edit menu. It will generate a c# class for the json. Magic.

Deserialize Json - Object of objects, This sample deserializes JSON to an object. Sample. Types. Copy. public class Account { public string Email { get; set; } public bool Active { get; set; } public  Next, the code creates a DataContractJsonSerializer object to deserialize the Customer object. It calls the serializer's ReadObject to read the object's serialization from the stream, casts the result into a Customer object, and returns the object. The ToString method shown in the following code is the last piece of the Customer class.

Okay, so the problem was in the .json file, it was not well structured, so I changed it to this:

[
    {
        "Id":"IdArticulo",
        "Fields":{"Origin":"IdArticulo","DestinationTable":"Articulos","DestinationField":"IdArticulo","DataType":"nvarchar"}
    },

    {
        "Id":"Nombre",
        "Fields":{"Origin":"Espanol","DestinationTable":"Articulos","DestinationField":"Descrip","DataType":"nvarchar"}
    }
]

And changed my C# code a bit and the end result is this:

using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace modules.dataProcessing.ConfManager
{
    public class Fields
    {
        public string Origin { get; set; }

        public string DestinationTable { get; set; }

        public string DestinationField { get; set; }

        public string DataType { get; set; }

        public override string ToString()
        {
            string finalString = "Origin: " + this.Origin + "\n" + "DestinationTable: " + this.DestinationTable + "\n";
            finalString += "DestinationField: " + this.DestinationField + "\n" + "DataType: " + this.DataType;

            return finalString;
        }

    }

    public class FieldConfs
    {
        public string Id
        {
            get; set;
        }

        public Fields Fields
        {
            get; set;
        }       

    }

    public static class JsonTools
    {
        public static List<FieldConfs> loadConf(string json)
        {
            List<FieldConfs> fieldConfs = new List<FieldConfs>();

            fieldConfs = JsonConvert.DeserializeObject<List<FieldConfs>>(json);

            return fieldConfs;
        }

        public static void saveConf(List<FieldConfs> confs, string fileToSave)
        {
            using (StreamWriter file = File.CreateText(fileToSave))
            {
                JsonSerializer serializer = new JsonSerializer();

                // Serializamos el objeto directamente en el stream de datos
                serializer.Serialize(file, confs);
            }
        }
    }
}

Now I am able to serialize the list and deserialize it, thanks anyway for the answers you posted since after reading them I found the ilumination required to solve this.

Thank you all, sincerely.

Deserialize an Object, NET object using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent text and back again by mapping the . To serialize a.NET object to JSON, you must create an instance of DataContractJsonSerializer. The constructor for this class has one parameter: a reference to the type of.NET object that is to be serialized or deserialized. Use the method WriteObject () to perform the serialization and send the output to the specified stream.

If you want the objects as list , then the json should be modified with square brackets which indicate them as a collection of objects .

{
    "fieldConfs" : 
        [
             {"origin" : "IdArticulo"    , "destinationTable" : "Articulos", "destinationField" : "IdArticulo", "dataType" : "nvarchar"},
             {"origin" : "Nombre"    , "destinationTable" : "Articulos", "destinationField" : "Descrip", "dataType" : "nvarchar"}
        ]
}

Serializing and Deserializing JSON, deserialize differently structured JSON objects to the same C# class. Then it loops through the properties (or attributes) of the JSON object  Adapting JSON Strings for Deserializing into C# Objects Introduction. Two recent projects have revolved around desrializing JSON strings returned by REST API endpoints into Background. To be suitable for deserialization into a C# object, a JSON string must look something like the following

JSON Serialization And Deserialization In C#, If that is your complete JSON string, then no, it's not going to work - it's incomplete and missing a few close brackets, both square and curly. Deserializing different JSON structures to the same C# class Introduction. During the development of the Auth0 .NET SDK, I came across an interesting scenario where some of the API Enter JsonConverter. Thankfully JSON.NET allows for a lot of flexibility in how JSON objects are deserialize. I

Deserializing different JSON structures to the same C# class, Deserialize(json); Can you help me understand the issue? I have made it a list as there can be situation to receive multiple objects and hence it is  If you use C# 2010 or newer, you can use dynamic type: dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring); Then you can access attributes and arrays in dynamic object using dot notation: string nemo = json.response[0].images[0].report.nemo;

Deserialization of JSON string to object list, We are going to deserialize a JSON file to a MyModel object instance. in a C# class will match with a key “ myProperty ” in the JSON object, and vice However, we occasionally need to deal with JSON objects that are not  Deserialize n-level(multi level) JSON data to C# custom object Read XML file in ASP.NET(C#) Creating a Graph in ASP.NET with AJAX, HttpHandler and Morris.js Plugin

Comments
  • any specific error you are getting ?
  • fieldConfs is probably better off as Dictionary<string, Field>. You also need a root object with the fieldConfs property.
  • Try to deserialize into this: public class Root { public Dictionary<string, Field> FieldConfs { get; } = new Dictionary<string, Field>(); } - ie. JsonConvert.DeserializeObject<Root>(json);.
  • For static purposes it does the work, but the creations of those Ids are dynamic, but thanks for your answer.
  • The way you use it you can make FieldConfs.Fields property simply of type Field instead of List<Field>.
  • Yeah, thats' what I did, I changed my C# code in the solution.
  • But mind that this results in a List<Field> instead of a List<FieldConfs>.
  • After the deserialization call FieldConfs fieldConfs = new FieldConfs() { Fields = listOfFields } where listOfFields is the the result of type List<Field>.
  • Also to get the collection you would have to get the value of fieldConfs first (the JSON array). Otherwise the deserialization will fail.
  • Thanks for updating.