locked
Json and C# - Windows Store App

    Question

  • hello everybody,

    i need some help with the development of this application, i am currently working on parsing a list of objects from a server response, well i was able to do most of the required stuff except that i got stuck on this problem.

    the json structure that i recieve ( the server is not mine ) is kinda weird but i was able to understand it better using the http://json2csharp.com/ website. but at the end i got stuck at getting the information of a sub-list in the objects that i receive.

    this is the Json Structure: 

    as you can see in the 2 objects above i have 2 objects ( obj 4 and obj 5), i am interested in the internal_link of these objects, as you can see one of them has an object inside it and the other one doesn't have anything, by now am good but i am unable to retrieve the information that is inside the object 5 internal_links ( unable to get the _id and the collection )

    using http://json2csharp.com/ i got this data structure:

    public class Data
    {
        public string file_name { get; set; }
    }
    
    public class Image
    {
        public string _id { get; set; }
        public string collection { get; set; }
        public Data data { get; set; }
    }
    
    public class RootObject
    {
        public string _id { get; set; }
        public string external_link { get; set; }
        public string height { get; set; }
        public List<Image> image { get; set; }
        public List<object> internal_link { get; set; }
        public string name { get; set; }
        public double sorting_order { get; set; }
        public string special_link { get; set; }
        public string width { get; set; }
        public string x_coordinate { get; set; }
        public object y_coordinate { get; set; }
    }

    well my problem is with the public List<object> internal_link {get; set;},,,,, what is this object type ?! and how can i use it to retrieve the _id and the collection ???

    this is what i found on the MSDN http://msdn.microsoft.com/en-us/library/system.object.aspx for this class but its of no use to me...

    Note: i have changed the data structure and modified it for the use of my program and this is the structure that i am using:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace royaJson
    {
        class JsonObjects
        {
            public string Title { get; set; }
            public string Image { get; set; }
            public bool show_List_Pages { get; set; }
            public bool video_List_Pages { get; set; }
            public int list { get; set; }
            public string list_ID { get; set; }
            public string Subtitle { get; set; }
        }
        public class jsonData
        {
            public string file_name { get; set; }
        }
    
        public class Image
        {
            public string _id { get; set; }
            public string collection { get; set; }
            public jsonData data { get; set; }
        }
    
        public class InternalObject
        {
            public string _ID { get; set; }
            public string _collection { get; set; }
        }
    
        public class RootObject
        {
            public string _id { get; set; }
            public string external_link { get; set; }
            public string height { get; set; }
            public List<Image> image { get; set; }
            public List<InternalObject> internal_link { get; set; }
            public string name { get; set; }
            public double sorting_order { get; set; }
            public string special_link { get; set; }
            public string width { get; set; }
            public string x_coordinate { get; set; }
            public object y_coordinate { get; set; }
        }
    }

    + this is the way i am trying to get the information:

            public async void getData()
            {
                HttpClient request = new HttpClient();
                // HttpResponseMessage msg = (HttpResponseMessage)request.GetStringAsync("http://xxxxxxxxxxxxxxxxxxxxx");
                HttpResponseMessage response = await request.GetAsync("http://xxxxxxxxxxxxxxxxxxxxxxxxx");
    
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    string responseString = await response.Content.ReadAsStringAsync();
                    // parse to json
                    //var group = JsonObject.Parse(responseString);
    
                    //itemGridView.ItemsSource = group;
                    var ds = new DataContractJsonSerializer(typeof(RootObject[]));
                    var msnew = new MemoryStream(Encoding.UTF8.GetBytes(responseString));
                    RootObject[] items = (RootObject[])ds.ReadObject(msnew);
                    List<JsonObjects> group = new List<JsonObjects>();
                    foreach (var ev in items)
                    {
                        string imagepath = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + ev.image[0].data.file_name;
                        JsonObjects menuItem = new JsonObjects();
                        menuItem.Image = imagepath;
                        menuItem.Title = ev.name;
                        try
                        {
                            if (ev.internal_link.Any())
                            {
                                menuItem.list = ev.internal_link.Count();
                                menuItem.list_ID = ev.internal_link[0]._ID;
                                menuItem.Subtitle = ev.internal_link[0]._collection;
                                if (ev.internal_link[0]._collection.Equals("show_list_pages"))
                                {
                                    menuItem.show_List_Pages = true;
                                    menuItem.video_List_Pages = false;
                                }
                                else
                                {
                                    menuItem.video_List_Pages = true;
                                    menuItem.show_List_Pages = false;
                                }
                            }
                        }
                        catch { }
                            group.Add(menuItem);
                        }
                        itemGridView.ItemsSource = group;
                    }
    
                }

    but its not working: as a proof here is what i see on the screen:

    my Xaml template:

    <DataTemplate x:Key="Standard250x250ItemTemplate">
            <Grid HorizontalAlignment="Left" Width="250" Height="250">
                <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
                    <Image Source="{Binding Image}" Stretch="None"  AutomationProperties.Name="{Binding Title}"/>
                </Border>
                <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
                    <TextBlock Text="{Binding list}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="60" Margin="15,0,15,0"/>
                    <TextBlock Text="{Binding Title}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
                </StackPanel>
            </Grid>
        </DataTemplate>

    i get some objects with 1 some with 0 but truly i cant retrieve any more information when i change the Binding from List to Subtitle nothing appears on screen.


    Saturday, May 17, 2014 4:38 PM

Answers

  • actually after thinking about it all day, i found out how stupid the answer is......

    well in case somebody falls in here, here is the answer:

    it is actually very case sensitive + the variable names that i had in my class of internal links was different from what is in the Json response,

    simply in the Json response, the variables are:

    _id

    collection

    meanwhile in my case i declared them as: _ID & _collection which made it not work

    now everything works perfectly

    :D

    • Marked as answer by Anton Thabain Saturday, May 17, 2014 9:28 PM
    Saturday, May 17, 2014 9:28 PM

All replies

  • actually after thinking about it all day, i found out how stupid the answer is......

    well in case somebody falls in here, here is the answer:

    it is actually very case sensitive + the variable names that i had in my class of internal links was different from what is in the Json response,

    simply in the Json response, the variables are:

    _id

    collection

    meanwhile in my case i declared them as: _ID & _collection which made it not work

    now everything works perfectly

    :D

    • Marked as answer by Anton Thabain Saturday, May 17, 2014 9:28 PM
    Saturday, May 17, 2014 9:28 PM
  • Thanks for sharing the solution here :) should help someone who meet the same problem.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, May 19, 2014 1:31 AM
    Moderator