0

Im using C# and Json.NET and i want to convert a Json string with arrays to a single class object. I tried using JsonConvert.Populate with JsonProperty DataAnnotation, but didnt worked

Here is an example of what i tried

JSON String:

{  
   "name":"julian",
   "card":{  
      "cardholder":{  
         "identification":{  
            "number":"32556188",
            "type":"DNI"
         },
         "name":"John"
      }
   }
}

C# Class:

public class Payment
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("card.cardholder.name")]
    public string CardHolderName { get; set; }

    [JsonProperty("card.cardholder.identification.number")]
    public string CardHolderIdentificationNumber { get; set; }
}

Code of Conversion:

var jsonString = ObtainJSONString();
var _payment = new Payment();
JsonConvert.PopulateObject(jsonString, _payment);
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
Julian Gr
  • 93
  • 1
  • 9
  • Please provide an example of your expected output. How are you expecting to convert an array of objects to a single object? –  Nov 07 '18 at 21:10
  • Do you just need to convert the json into a C# object? Or do you need to do the opposite thing as well (convert object to json string)? – Rui Jarimba Nov 07 '18 at 21:14
  • This one might help you: https://stackoverflow.com/questions/35628318/deserialize-nested-json-to-a-flat-class-using-json-net – Reno Nov 07 '18 at 21:23

2 Answers2

1

What you need is called deserialization and I am personally using Newtonsoft Json, then you can use this syntax:

var paymentObject = JsonConvert.DeserializeObject<Payment>(jsonString );

https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

Jiří Herník
  • 2,412
  • 1
  • 25
  • 26
1

Assuming the following model:

public class Payment
{
    public string Name { get; set; }
    public string CardHolderName { get; set; }
    public string CardHolderIdentificationNumber { get; set; }
}

You can parse the json string like this:

string json = @"{  
   ""name"":""julian"",
   ""card"":{  
      ""cardholder"":{  
         ""identification"":{  
            ""number"":""32556188"",
            ""type"":""DNI""
         },
         ""name"":""John""
      }
   }
}";

var token = JObject.Parse(json);

var payment = new Payment
{
    Name = token["name"].ToString(),
    CardHolderName = token["card"]["cardholder"]["name"].ToString(),
    CardHolderIdentificationNumber = token["card"]["cardholder"]["identification"]["number"].ToString()
};
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
  • Thank you! i will use that method. I thought that there was a more automated solution, like a decorator or something like that, to specifiy the map. – Julian Gr Nov 08 '18 at 12:27
  • I just tried it and the indexers doesnt work – Julian Gr Nov 08 '18 at 13:35
  • @JulianGr what do you mean by "it doesn't work"? Any errors? Or you just aren't able to retrieve the values? I've tested with the json sample in the question, and it worked properly. – Rui Jarimba Nov 08 '18 at 16:12
  • With the indexer is not possible to get the value, i had to use the method SelectToken. Like this : token.SelectToken("card.cardholder.name"); – Julian Gr Nov 12 '18 at 01:03