0

In MyClass I have property JsonArrayString. It is array of json objects.

What is best way to convert this string into array of MyJsonObject class in ASP.NET MVC Core application.

P.S. I can change structure of json string.

class MyClass {
     public string JsonArrayString => "[{"Value": "1", "Name": "One"}, ...]";
     public List<MyJsonObject> JsonArray => ...
}

public class MyJsonObject {
    string Value { get; set; }
    string Name { get; set; }
}

Result:

// First element of JsonArray => MyJsonObject() { Value = "1", "Name" = "One"};
  • Read my question – Mate Gvenetadze Sep 27 '19 at 08:31
  • Best way of ... => primarily opinion based => offtopic - please reword your question – Sir Rufo Sep 27 '19 at 08:36
  • 3
    It´s weird that your data-objects contain **json**-strings. – MakePeaceGreatAgain Sep 27 '19 at 08:41
  • @MateGvenetadze read the first answer of the question. I feel like its a duplicate :) – JieBaef Sep 27 '19 at 09:06
  • Where does this `JsonArrayString`? Why is it a string, and not direct result from the deserialization of this string? Do you have enough access to the cached string version to justify it existence? Do we really need a deserialization of the original string every time we call the `MyClass .JsonArray`? we won't be able to persist any change without changing the string. – xdtTransform Sep 27 '19 at 09:18
  • Form me this is an X/Y problem, You know that's not the correct way and looking for an alternative but instead of showing the big picture. You show your try without context. We can only address what we are told. For this question in it's for the solution is the Duplicate target. Perhaps you can try to answer our question, to see if we can understand what you really trying to do. Basically retry the [ask] guideline. – xdtTransform Sep 27 '19 at 09:22

2 Answers2

3

I recommend to install Newtonsoft.Json via the NuGet package manager. Then decorate the members of the POCO class with the JsonProperty attribute to tell the serializer which property matches to a specific JSON key:

public class MyJsonObject
{
  [JsonProperty("Value")]
  string Value { get; set; }

  [JsonProperty("Name")]
  string Name { get; set; }
}

You can the deserialize the JSON to a corresponding object instance:

var jsonString = "[{\"Value\": \"1\", \"Name\": \"One\"}, {\"Value\": \"2\", \"Name\": \"Two\"}]";
List<MyJsonObject> myJsonObjects = JsonConvert.DeserializeObject<List<MyJsonObject>>(jsonString);
BionicCode
  • 1
  • 4
  • 28
  • 44
0

You have to add Newtonsoft.Json via the NuGet package manager.

public class MyClass
{
    public string JsonArrayString => "[{\"Value\": \"1\", \"Name\": \"One\"}, {\"Value\": \"2\", \"Name\": \"Two\"}]";
    public List<MyJsonObject> JsonArray => JsonConvert.DeserializeObject<List<MyJsonObject>>(JsonArrayString);
}

public class MyJsonObject
{
    [JsonProperty("Value")]
    string Value { get; set; }
    [JsonProperty("Name")]
    string Name { get; set; }
}

class Program
{

    static void Main(string[] args)
    {
        var myClass = new MyClass();
    }
}

You can access the json array with myClass.JsonArray