Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
C#
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
Updated 8-Nov-12 8:26am
v3

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

C#
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();
 
Share this answer
 
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.
Hi,
Look at NewtonSoft.Json, it allows you to serialize and deserialize objects incredibly easily.
Can be installed via NuGet
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900