locked
JSON Deserialization from premade .JSON File RRS feed

  • Question

  • User2086475856 posted

    I hope I'm posting this in the correct space and if not if someone can move it if they have that ability or just post back with where this belongs I'm happy to post in the correct area!

    I have a project I'm working on that I need to pull a member from a JSON file. The file is premade but later will have a random member's values pulled.

    My file looks something like this:

    {
      "Member 0": {
        "Date": "Date",
        "Name": "First and Last Name",
        "Image": "Path to Image",
        "Content": "Content"
      },
      "Member 1": {
        "Date": "Date",
        "Name": "First and Last Name",
        "Image": "Path to Image",
        "Content": "Content"
      },
      "Member 2": {
        "Date": "Date",
        "Name": "First and Last Name",
        "Image": "Path to Image",
        "Content": "Content"
      }
    }

    Basically this will have unique data in it once I have it working, it will expand more the the 3 members that are in there it's just like that for testing.

    What I want to do exactly with this file is pull a random member and use the Date, Name, Image, and Content string data to insert into my webpage.

    I am trying to use version 11 of the built in JSON.NET Framework in ASP.NET Core 2.1 also am using Razor pages.

    Friday, June 28, 2019 7:43 PM

All replies

  • User-854763662 posted

    Hi fatalerror0x00 ,

    Try to change your json to an array of object like below :

    {
      "Members":[
        {
        "Date": "Date1",
        "Name": "First and Last Name",
        "Image": "Path to Image",
        "Content": "Content1"
      },
      {
        "Date": "Date2",
        "Name": "First and Last Name",
        "Image": "Path to Image",
        "Content": "Content2"
      },
       {
        "Date": "Date3",
        "Name": "First and Last Name",
        "Image": "Path to Image",
        "Content": "Content3"
      }
     ]
    }

    then you need a view model of IEnumerable type property Member to deserialize like:

       public class MemberVM
        {
            public IEnumerable<Member> Members { get; set; }
        }
        public class Member
        {
            public string Date { get; set; }
            public string Name { get; set; }
            public string Image { get; set; }
            public string Content { get; set; }
        }

    and then you could deserialize by using above view model like

    string data = System.IO.File.ReadAllText("D:\\MemberData.txt");
    MemberVM res = JsonConvert.DeserializeObject<MemberVM>(data);

    Reference :  https://stackoverflow.com/a/51360293/10201850 

    Best Regards ,

    Sherry

    Monday, July 1, 2019 7:00 AM
  • User-474980206 posted

    if your json is map rather than an array, you can deserialize to a dictionary:

        public class Member
        {
            public string Date { get; set; }
            public string Name { get; set; }
            public string Image { get; set; }
            public string Content { get; set; }
        }
    
        var obj = JsonConvert.DeserializeObject<Dictionary<string, Member>>(json);
    
    


     

    Monday, July 1, 2019 3:17 PM