Click here to Skip to main content
12,354,804 members (60,010 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# VS2010 JSON
Hi,

I'm very new to this, so I really hope there is somebody smart out there that will help me.

My JSON file looks like this (index3.json):
{"weeks": [{ "week": 1, "mothertext":"Det her er uge 1 mor", "babytext": "Det her er uge 1 baby" } ,{ "week": 2, "mothertext":"Det her er uge 2 mor", "babytext": "Det her er uge 2 baby" } ,{ "week": 3, "mothertext":"Det her er uge 3 mor", "babytext": "Det her er uge 3 baby" } ]}

I want to create an array in visual studio so its possible to insert the text from the different properties. I.e. week.mothertext[1] (or how it would look like) would deliver a string with "Det her er uge 2 mor" and so on.

This is my code which I must admit is losely constructed from code I have found on the web.
public InformationPage()
{
   InitializeComponent();
}
 
public class weeksArray
{
   public weekProperties[] weeklist { get; set; }
}
 
public class weekProperties
{
   public int week { get; set; }
   public string mothertext { get; set; }
   public string babytext { get; set; }
}
 
private void myButton1_Click(object sender, RoutedEventArgs e)
{
 
   var client = new WebClient();
 
   client.OpenReadCompleted += 
      ( s, eargs ) = >
      {
         var serializer = new DataContractJsonSerializer(typeof(weeksArray));
         var dataArray = (weeksArray)serializer.ReadObject(eargs.Result);
 
         foreach(var obj in dataArray.weeks)
             //This sort of works but doesnt allow me to retrieve the indidual objects like the
            //the text for week: 3 only.
         {
            textBoxWeek0.TextWriter = textBoxWeek0.Text + obj.week;
         }
      };
   var uri = new Uri("http://...../index3.json");
   client.OpenReadAsync( uri );
}
Posted 8-Nov-12 8:22am
Edited 8-Nov-12 8:26am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Preview
Hi Kasper,

I may be on the wrong track here, but please find below a few lines that work with your json.

class Program
{
    static void Main(string[] args)
    {
        string json = "[{\"week\":\"1\",\"mothertext\":\"Det her er uge 1 mor\",\"babytext\":\"Det her er uge 1 baby\"},{\"week\":\"2\",\"mothertext\":\"Det her er uge 2 mor\",\"babytext\":\"Det her er uge 2 baby\"},{\"week\":\"3\",\"mothertext\":\"Det her er uge 3 mor\",\"babytext\":\"Det her er uge 3 baby\"}]";
 
        List<Week> weeks = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Week>>(json);
        foreach (Week week in weeks)
        {
            Console.WriteLine("Week {0} - {1} - {2}", week.week, week.mothertext, week.babytext);
        }
        Console.ReadLine();
    }
}
public class Week
{
    public string week { get; set; }
    public string mothertext { get; set; }
    public string babytext { get; set; }
}

Example use of lambda expression to filter results

var filteredWeeks = weeks.Where(x => x.week == "2");
            foreach (Week week in weeks)
            {
                Console.WriteLine("Week {0} - {1} - {2}", week.week, week.mothertext, week.babytext);
            }
            Console.WriteLine("Filtered");
            foreach (Week filteredWeek in filteredWeeks)
            {
                Console.WriteLine("Week {0} - {1} - {2}", filteredWeek.week, filteredWeek.mothertext, filteredWeek.babytext);
            }
            Console.ReadLine();
  Permalink  
v2
Comments
Kasper Andersen 8-Nov-12 17:20pm
   
Really nice. It shortened my code quite a bit but how will I be able to call individual weeks or mothertext or babytext?
As I see it the json contains an array with 3 different weeks, 3 diff mothertext and 3 diff babytext.
Btw im using listbox to display the output. (got it working with stringbuilder though) to show your answer
Kasper Andersen 9-Nov-12 4:48am
   
Hi Mick.

Amazing. That work perfectly!!
Thank youy so much. I was really stuck there.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,
Look at NewtonSoft.Json, it allows you to serialize and deserialize objects incredibly easily.
Can be installed via NuGet
  Permalink  
Comments
Kasper Andersen 8-Nov-12 16:12pm
   
I'm actually using NewtonSoft.Json...
I just can't get it to work properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100