Search code examples
c#jsonxamarinjson.netsystem.net.httpwebrequest

Data from GetResponseStream


How can I get the sub_category_en on this data I receive from HttpWebRequest? I just want the two to three fields on the data received.

02-14 16:33:56.793 I/mono-stdout( 8974): { 02-14 16:33:56.795 I/mono-stdout( 8974): "active": 1, 02-14 16:33:56.797 I/mono-stdout( 8974): "child_level": 1, 02-14 16:33:56.798 I/mono-stdout( 8974): "create_date": "2015-08-27T17:24:58.19+03:00", 02-14 16:33:56.798 I/mono-stdout( 8974): "description_ar": "", 02-14 16:33:56.800 I/mono-stdout( 8974): "description_en": "", 02-14 16:33:56.802 I/mono-stdout( 8974): "has_child": 1, 02-14 16:33:56.805 I/mono-stdout( 8974): "id": 1881, 02-14 16:33:56.805 I/mono-stdout( 8974): "id_category": 3, 02-14 16:33:56.808 I/mono-stdout( 8974): "id_parent": 0, 02-14 16:33:56.811 I/mono-stdout( 8974): "id_parents": "0", 02-14 16:33:56.811 I/mono-stdout( 8974): "id_user": 1, 02-14 16:33:56.811 I/mono-stdout( 8974): "last_update": "2015-08-27T17:24:58.19+03:00", 02-14 16:33:56.814 I/mono-stdout( 8974): "last_updated_by": 1, 02-14 16:33:56.815 I/mono-stdout( 8974): "meta_keyword_ar": "", 02-14 16:33:56.815 I/mono-stdout( 8974): "meta_keyword_en": "", 02-14 16:33:56.815 I/mono-stdout( 8974): "order_by": 94, 02-14 16:33:56.815 I/mono-stdout( 8974): "sub_category_ar": "Trailers", 02-14 16:33:56.815 I/mono-stdout( 8974): "sub_category_en": "Trailers", 02-14 16:33:56.816 I/mono-stdout( 8974): "sub_category_name_parents_ar": null, 02-14 16:33:56.816 I/mono-stdout( 8974): "sub_category_name_parents_en": null, 02-14 16:33:56.816 I/mono-stdout( 8974): "sub_category_url": "trailers", 02-14 16:33:56.816 I/mono-stdout( 8974): "sub_category_url_parents": null 02-14 16:33:56.816 I/mono-stdout( 8974): }, 02-14 16:33:56.817 I/mono-stdout( 8974): { 02-14 16:33:56.817 I/mono-stdout( 8974): "active": 1, 02-14 16:33:56.817 I/mono-stdout( 8974): "child_level": 1, 02-14 16:33:56.817 I/mono-stdout( 8974): "create_date": "2015-08-27T17:25:14.31+03:00", 02-14 16:33:56.817 I/mono-stdout( 8974): "description_ar": "", 02-14 16:33:56.818 I/mono-stdout( 8974): "description_en": "", 02-14 16:33:56.818 I/mono-stdout( 8974): "has_child": 1, 02-14 16:33:56.818 I/mono-stdout( 8974): "id": 1882, 02-14 16:33:56.818 I/mono-stdout( 8974): "id_category": 3, 02-14 16:33:56.818 I/mono-stdout( 8974): "id_parent": 0, 02-14 16:33:56.818 I/mono-stdout( 8974): "id_parents": "0", 02-14 16:33:56.819 I/mono-stdout( 8974): "id_user": 1, 02-14 16:33:56.819 I/mono-stdout( 8974): "last_update": "2015-08-27T17:25:14.31+03:00", 02-14 16:33:56.819 I/mono-stdout( 8974): "last_updated_by": 1, 02-14 16:33:56.819 I/mono-stdout( 8974): "meta_keyword_ar": "", 02-14 16:33:56.819 I/mono-stdout( 8974): "meta_keyword_en": "", 02-14 16:33:56.820 I/mono-stdout( 8974): "order_by": 95, 02-14 16:33:56.820 I/mono-stdout( 8974): "sub_category_ar": "Aeroplanes", 02-14 16:33:56.820 I/mono-stdout( 8974): "sub_category_en": "Aeroplanes", 02-14 16:33:56.820 I/mono-stdout( 8974): "sub_category_name_parents_ar": null, 02-14 16:33:56.820 I/mono-stdout( 8974): "sub_category_name_parents_en": null, 02-14 16:33:56.821 I/mono-stdout( 8974): "sub_category_url": "aeroplanes", 02-14 16:33:56.821 I/mono-stdout( 8974): "sub_category_url_parents": null 02-14 16:33:56.821 I/mono-stdout( 8974): } 02-14 16:33:56.821 I/mono-stdout( 8974): ]

I used xamarin in visual studio 2013.. Thanks for helping. If using this Newtonsoft.Json.JsonSerializer is the ey on my problem. Can I have a small sample code? thanks


Solution

  • You can use JsonConvert.Deserialize to convert json to a custom object.

    public class RootObject
    {
        public int id { get; set; }
        public string sub_category_name { get; set; }
        public int child_level { get; set; }
        public int has_child { get; set; }
        public string sub_parent_url { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
          WebRequest objRequest = HttpWebRequest.Create(dest);
          WebResponse objResponse = objRequest.GetResponse();
          using (StreamReader reader = new StreamReader(objResponse.GetResponseStream()))
          {
            string jsonString = reader.ReadToEnd();
    
            var data = JsonConvert.DeserializeObject<List<RootObject>>(jsonString);
    
          }
        }
    }