0

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

BlueAries
  • 31
  • 1
  • 8

1 Answers1

0

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);

      }
    }
}
sjokkogutten
  • 2,005
  • 2
  • 21
  • 24
  • I am having error on JArray data = JArray.Parse(jsonString); jsonString= "\"[{\\\"id\\\":44,\\\"sub_category_name\\\":\\\"Cars For Sale\\\",\\\"child_level\\\":1,\\\"has_child\\\":1,\\\"sub_parent_url\\\":null,\\\"sub_cat_url\\\":\\\"cars-for-sale\\\"},{\\\"id\\\":892,\\\"sub_category_name\\\":\\\"Cars for Rent\\\",\\\"child_lev......... I get that jsonString from var content = reader.ReadToEnd(); – BlueAries Feb 17 '16 at 09:21
  • You are getting an error when you are reading your json. I have updated the answer, assuming you are reading from a file – sjokkogutten Feb 17 '16 at 09:41
  • If you are hard-coding the values into `jsonString`, you do not need to use `reader.ReadToEnd()` – sjokkogutten Feb 17 '16 at 09:45
  • no I'm not hardcoding the valuw of jsonstring. I just give the data of jasonstring that I get from the httwebresponse – BlueAries Feb 17 '16 at 10:12
  • Could you update the question with the code you are using to read the json? – sjokkogutten Feb 17 '16 at 10:49
  • I want o get the data of sub_category_en and sub_category_url. I am using HttpWebResponse response = request.GetResponse() as HttpWebResponse; StreamReader reader = new StreamReader(response.GetResponseStream());var content = reader.ReadToEnd(); – BlueAries Feb 17 '16 at 10:51
  • See updated answer. Are you sure you are receiving a valide json? Use http://pro.jsonlint.com/ to validate your json. – sjokkogutten Feb 17 '16 at 11:02
  • JArray data = JArray.Parse(jsonString); where is the jsonString in yur sample code? – BlueAries Feb 17 '16 at 11:06
  • I tried the json that I am getting from your link. the json data is valid – BlueAries Feb 17 '16 at 11:09
  • I get this error "Newtonsoft.Json.JsonReaderException: Error reading JArray from JsonReader. Current JsonReader item is not an array: String. Path '', line 1, position 1574." pointing at JArray data = JArray.Parse(json); – BlueAries Feb 17 '16 at 11:21
  • Could you send med the url? – sjokkogutten Feb 17 '16 at 11:24
  • http://192.168.1.136:2504/Service1.svc/getsub_category/0/1/Motors that's the sample – BlueAries Feb 17 '16 at 11:25
  • I am unable to connect to the server. But using `HttpWebRequest` should be able to consume your service. Have a look at this article: http://codewala.net/2014/01/06/how-to-consume-wcf-rest-services-using-c/ – sjokkogutten Feb 17 '16 at 11:43
  • this is the json data I get "[{\"id\":44,\"sub_category_name\":\"Cars For Sale\",\"child_level\":1,\"has_child\":1,\"sub_parent_url\":null,\"sub_cat_url\":\"cars-for-sale\"},{\"id\":896,\"sub_category_name\":\"Motorcycles\",\"child_level\":1,\"has_child\":1,\"sub_parent_url\":null,\"sub_cat_url\":\"motorcycles\"},{\"id\":897,\"sub_category_name\":\"Cars Plates For Sale\",\"child_level\":1,\"has_child\":0,\"sub_parent_url\":null,\"sub_cat_url\":\"cars-plates-for-sale\"}]" – BlueAries Feb 17 '16 at 12:02
  • I am guessing the problem is that you have double set of JSON encoding, hence all the backslashes. http://stackoverflow.com/questions/3078397/returning-raw-json-string-in-wcf. The code works fine when I read it locally without the backslashes, but break with them. – sjokkogutten Feb 17 '16 at 12:02
  • double set of JSON encoding in Web Service? no, Its only a single json return. And I receive "[{\"id\":44,\"sub_category_name\":\"Cars For Sale\"... data of like that. the problem now is on the JArray data = JArray.Parse(json); saying Current JsonReader item is not an array. – BlueAries Feb 17 '16 at 12:08
  • Thank you very much sjokkogutten – BlueAries Feb 17 '16 at 12:37
  • No worries mate. Let's try a different approach, see the updated answer – sjokkogutten Feb 17 '16 at 12:54