0

I am having the JSON object which is stored in to in to the Variable. The JSON looks like

{
 "data":
[
{"SNumber":"05",
 "LName":"TyJ",
 "LNameMarkup":"18/TyJ"
}]}

I created the Model class for the JSON like

namespace SetName.Models
{
public class SNameDTO
{     
 public class Datum
 {
  public string SNumber { get; set; }
  public string LName { get; set; }
  public string LNameMarkup { get; set; }
 }

 public class RootObject
 {
  public List<Datum> data { get; set; }
 }
 }
 }

Now I want to deserialize this JSON I can get the LName from it

  var retSName = GetSName(StockNumber);   //stores the JSON shown above

I replaced the above code with

  SNameDTO.RootObject retSName = GetSName(StockNumber);

It throws error like

Cannot implicitly convert type 'string' to 'SetName.Models.SNameDTO.RootObject'

I am trying to use the Model Class instead of the dynamic objects.

xyz
  • 531
  • 1
  • 10
  • 31
  • 2
    What's the code in that GetSName? – Renato Afonso Oct 30 '17 at 17:18
  • Possible duplicate of [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – newfurniturey Oct 30 '17 at 17:19
  • @RenatoAfonso that is the method that returns me the JSON I posted above. – xyz Oct 30 '17 at 17:19
  • @newfurniturey that uses the dynamic Objects. I am seeing if I can use the Model class. If I can use the Model class how can I approach this – xyz Oct 30 '17 at 17:23
  • You are not deserializing anything, your just trying to dump a string (your JSON is a string) into a model, and that won't work. – oerkelens Oct 30 '17 at 17:25
  • @oerkelens I am new to this not sure how to use Model class deserializing the JSON variable – xyz Oct 30 '17 at 17:27

2 Answers2

1

To actually deserialize your JSON string, you need to do a bit more. If you use Json.Net (available on NuGet) you can try something like:

var myClass = JsonConvert.DeserializeObject<RootObject>(GetSName(StockNumber));
oerkelens
  • 5,053
  • 1
  • 22
  • 29
  • How do I access the LName from the RootObject – xyz Oct 30 '17 at 17:30
  • @xyz That is a property you have defined yourself... how about `myClass.data.First().Lname` if you want the Lname of the first data object in your list? That has little to do with json or (de)serialization. I suggest you first check in debug if your deserialization works. – oerkelens Oct 30 '17 at 17:33
0

Used Newtonsoft JSON nuget package, able to Deserialize it as below, Hope this helps.

class Program
{
    static void Main(string[] args)
    {
        var json = "{\"SNumber\":\"05\",\"LName\":\"TyJ\",\"LNameMarkup\":\"18/TyJ\"}";

        var jsonCollection =
            "[{\"SNumber\":\"05\",\"LName\":\"TyJ\",\"LNameMarkup\":\"18/TyJ\"},\r\n{\"SNumber\":\"10\",\"LName\":\"TyJ2\",\"LNameMarkup\":\"20/TyJ\"}]";

        var jsonRootObject = "{ \"data\" : [{\"SNumber\":\"05\",\"LName\":\"TyJ\",\"LNameMarkup\":\"18/TyJ\"}, {\"SNumber\":\"10\",\"LName\":\"TyJ2\",\"LNameMarkup\":\"20/TyJ\"}]}";

        var data = JsonConvert.DeserializeObject<SNameDTO.Datum>(json);
        var dataCollection = JsonConvert.DeserializeObject<List<SNameDTO.Datum>>(jsonCollection);
        var rootObject = JsonConvert.DeserializeObject<SNameDTO.RootObject>(jsonRootObject);
    }
}


public class SNameDTO
{
    public class Datum
    {
        public string SNumber { get; set; }
        public string LName { get; set; }
        public string LNameMarkup { get; set; }
    }

    public class RootObject
    {
        public List<Datum> data { get; set; }
    }
}
Viju
  • 95
  • 1
  • 7
  • If you don't want to install additional packages, the .NET JavaScriptSerializer works perferctly on JSON objects. JavaScriptSerializer.Deserialize() method converts your JSON to an object of any type. Just add a reference to the System.Web.Extensions assembly. – Alex X. Oct 30 '17 at 17:50