none
JSON Deserialization from premade .JSON File RRS feed

  • Question

  • 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.2 also am using Razor pages.



    Friday, June 28, 2019 6:16 PM

All replies

  • The data looks like the data belongs in a Json array. 

    https://www.mikesdotnetting.com/article/318/working-with-json-in-razor-pages

    I have not used razor pages. So you best post to MVC forum in ASP.NET forums.

    http://forums.asp.net/

    Friday, June 28, 2019 7:08 PM
  • Hello,

    See if the following post might nudge you in the right direction.

    https://forums.asp.net/t/2037761.aspx?How+to+deserialize+json+array+ 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, June 28, 2019 8:21 PM
    Moderator
  • Hi fatalerror0x00,

    Thank you for posting here.

    For your question, you want to achieve the random member from the above JSON string.

    I noted that the Json you provided is not regular, so I suggest that you could use the following modified json.

    {
      "Members":{
       "Member 0": {
        "Date": "Date1",
        "Name": "First and Last Name",
        "Image": "Path to Image",
        "Content": "Content1"
      },
      "Member 1": {
        "Date": "Date2",
        "Name": "First and Last Name",
        "Image": "Path to Image",
        "Content": "Content2"
      },
      "Member 2": {
        "Date": "Date3",
        "Name": "First and Last Name",
        "Image": "Path to Image",
        "Content": "Content3"
      }
     }
    }

    Code:

     class Program
        {
            public class Test
            {
                public Dictionary<string,Member> Members{get;set;}
            }
            public class Member
            {
                public string Date { get; set; }
    
                public string FirstName { get; set; }
                public string Image { get; set; }
                public string Content { get; set; }
    
    
            }
            static void Main(string[] args)
            {
                string text = File.ReadAllText("D:\\test.txt");
                var res = JsonConvert.DeserializeObject<Test>(text);
                foreach (var f in RandomValues(res.Members).Take(1))
                {
                    Console.WriteLine(f.FirstName+"  "+f.Date+"  "+f.Image+"  "+f.Content );
                }
                Console.ReadKey();
            }
    
            public static IEnumerable<TValue> RandomValues<TKey, TValue>(IDictionary<TKey, TValue> dict)
            {
                Random rand = new Random();
                List<TValue> values = Enumerable.ToList(dict.Values);
                int size = dict.Count;
                while (true)
                {
                    yield return values[rand.Next(size)];
                }
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 1, 2019 3:10 AM
    Moderator