Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Tuesday, May 23, 2017

wcf JSON deserialization

this
new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(results)
will not work with complex objects

you need to create objects

for example, this is the IManage Login JSON

[DataContract]
    public class IManageUser
    {
        [DataMember(Name = "email")]
        public string email { get; set; }
        [DataMember(Name = "full_name")]
        public string full_name { get; set; }
        [DataMember(Name = "user_id")]
        public string user_id { get; set; }
    }

    [DataContract]
    public class IManageLoginInfo
    {
        [DataMember(Name = "X-Auth-Token")]
        public string X_Auth_Token { get; set; }
        [DataMember(Name = "max_age")]
        public int max_age { get; set; }
        [DataMember(Name = "persona")]
        public string persona { get; set; }
        [DataMember(Name = "user")]
        public IManageUser user { get; set; }

    }

then you could do something like this:
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                results = sr.ReadToEnd();
                sr.Close();
                IManageLoginInfo info = null;
                using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(results)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IManageLoginInfo));
                    info = (IManageLoginInfo)serializer.ReadObject(ms);
                }
                this.token = info.X_Auth_Token;

Tuesday, July 26, 2016

simple json call from c#

string URL = "http://www.blah.com/";
                    string urlParameters = “?v=1”;
                   

                    HttpClient client = new HttpClient();
                    client.BaseAddress = new Uri(URL);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = client.GetAsync(urlParameters).Result; 
                    if (response.IsSuccessStatusCode)
                    {
                        string dataObjects = response.Content.ReadAsStringAsync().Result;
                        var jss = new JavaScriptSerializer();
                        var dict = jss.Deserialize<Dictionary<string, dynamic>>(dataObjects);

                        foreach (Dictionary<string, object> x in dict["items"])

                        {

Thursday, January 15, 2015

Getting values out of a JSON object in c# when the object is an array


                         var values =    JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(strVal);
                         foreach (var val in values)
                           {
                               blah.Add(int.Parse(val.First(x => x.Key.Equals("blahId")).Value.ToString()));

  

Wednesday, February 12, 2014

Remove json syntax from strings

with JavaScriptSerializer class

 JavaScriptSerializer javaScriptDeserializer = new JavaScriptSerializer();
                List<string> idList = javaScriptDeserializer.Deserialize<List<String>>(Ids);