C# json deserialize to (this) inside object

c# validate json before deserialize
newtonsoft json deserialize
deserialize multiple json objects c#
deserialize json to object c#
jsonconverter attribute
c# json deserialize object to model
deserialize json to dynamic object c# using newtonsoft
json to c#

I am trying to get an object to deserialize into itself. I have tride the following:-

   public class JobID
    {
        public string jobname;
        public string first;
        public string second;
        public string third;
        public string clientName;
        public string workflow;
    }

    public void load(string fname)
    {
        string s = File.ReadAllText(fname);
        this = JsonConvert.DeserializeObject<JobID>(s);    
    }

But the word this is 'read only' according to the error I get. I have used 'this.jobname = "X";' before so clearly 'this' is not read only. I am using Newtonsof.Json.

Why not use static method to load the object. Such as :

public class JobID
{
    public string jobname;
    public string first;
    public string second;
    public string third;
    public string clientName;
    public string workflow;
    public static JobId Load(string fname){
        string s = File.ReadAllText(fname);
        return JsonConvert.DeserializeObject<JobID>(s);    
    }
}

Deserialize array of json objects with json object inside, There is one cool feature in VS2013. If you copy your JSON to clipboard and in Visual Studio click EDIT -> Paste Special -> Paste JSON as� 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.

Although you can assign a value to a property of 'this', you can't change the object to which 'this' refers to.

The 'this' keyword refers to the current object instance in the context (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this).

Assuming the Load function is inside another object (or static), you could do something like:

 public class JobID
{
    public string jobname;
    public string first;
    public string second;
    public string third;
    public string clientName;
    public string workflow;
}

public class JobReader
{
 // Property to store deserialized object
 public JobID Job { get; set; }

 public void load(string fname)
 {
     string s = File.ReadAllText(fname);

     // Assign object to property.
     this.JobID = JsonConvert.DeserializeObject<JobID>(s);    
 }
} 

Deserializing different JSON structures to the same C# class, The JsonConverter class allows you to deserialize differently structured JSON objects to the same C# class. In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom .Net object. In the following code, it calls the static method DeserializeObject () of the JsonConvert class by passing JSON data. It returns a custom object (BlogSites) from JSON data. string json = @" {.

You can use the static method as suggested by Xbotter to create a new instance. But for some reason if you want to deserialize the file content into current object only, then easiest way would be:

public class JobID
{
    public string jobname;
    public string first;
    public string second;
    public string third;
    public string clientName;
    public string workflow;

    public void load(string fname)
    {
        string s = File.ReadAllText(fname);
        JobID tmp = JsonConvert.DeserializeObject<JobID>(s);
        copy(tmp);
    }

    public void copy(JobID tmp)
    {
        this.jobname = tmp.jobname;
        // do the same for other properties that you want to copy
    }
}

JSON deserialization : multiple object inside, An easy way is to use a service that can convert JSON into C# classes. public class Data { [JsonProperty("EmpDeptId")] public string� For an introduction to System.Text.Json, see How to serialize and deserialize JSON in .NET. A converter is a class that converts an object or a value to and from JSON. The System.Text.Json namespace has built-in converters for most primitive types that map to JavaScript primitives. You can write custom converters:

If you want to do this without a static method, you can use the JsonSerializer.Populate method. Example code:

public class JobID
{
    public string jobname;
    public string first;
    public string second;
    public string third;
    public string clientName;
    public string workflow;

    public void load(string fname)
    {
        string s = File.ReadAllText(fname);
        new JsonSerializer().Populate(new JsonTextReader(new StringReader(s)), this);
    }
}

JsonSerializer.Deserialize Method (System.Text.Json), Reads one JSON value (including objects or arrays) from the provided reader into an instance of the type specified by a generic type parameter. Deserialize(� C# json deserialize to (this) inside object. Ask Question Asked 1 year, 11 months ago. Active 4 days ago. Viewed 346 times 3. I am trying to get an object to

Serialization (C#), Json namespace contains classes for JavaScript Object Notation (JSON) serialization and deserialization. JSON is an open standard that is� Deserialize an Object This sample deserializes JSON to an object. Sample. Types. Copy. public class Account

Deserialize a Collection, string json = @"['Starcraft','Halo','Legend of Zelda']"; List<string> videogames = JsonConvert.DeserializeObject<List<string>>(json); Console.WriteLine(string. Use ReadObject() to deserialize an object from a JSON stream. Classes and Service Contracts DataContracts are agreements between clients and servers that describe the information that is to be exchanged between them.

Serializing Collections, To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for. Json. Thankfully JSON.NET allows for a lot of flexibility in how JSON objects are deserialize. I have written two blog posts on this before. To handle this particular scenario, I created a JsonConverter with a simple mapping table which maps the attributes of the second JSON payload to the attributes of the first JSON payload.

Comments
  • C and C# are two different languages. Please use correct tags.
  • I learnt something new - at least how to use static in this way - thank you.
  • I understand now, because JsonConvert creates the object, not just populating it. (Light goes on!)