Link to home
Start Free TrialLog in
Avatar of kirannandedkar
kirannandedkar

asked on

getting error while converting to double

I want to convert jtoken value to double. How can i do it. here is code,
var responseStream = webRequest.GetResponse().GetResponseStream();
                Encoding encode = System.Text.Encoding.Default;


                using (StreamReader reader = new StreamReader(responseStream, encode))
                {
                    JToken token = JObject.Parse(reader.ReadToEnd());
                    var pagination = token.SelectToken("pagination");

                    if (pagination != null && pagination.SelectToken("next_url") != null)
                    {
                        nextPageUrl = pagination.SelectToken("next_url").ToString();
                    }
                    else
                    {
                        nextPageUrl = null;
                    }

                    var images = token.SelectToken("data").ToArray();

                    foreach (var image in images)
                    {
                        imageUrl = image.SelectToken("images").SelectToken("standard_resolution").SelectToken("url").ToString();

                        if (String.IsNullOrEmpty(imageUrl))
                            Console.WriteLine("broken image URL");
                     
                        var webClient = new WebClient();
                        byte[] imageBytes = webClient.DownloadData(imageUrl);

                        
                        var imageId = image.SelectToken("id");
                        String sharePointSite = "http://mysite/";
                        String documentLibraryName = "myimage";

                            using (var site = new SPSite(sharePointSite))
                            {
                                using (SPWeb web = site.OpenWeb())
                                {
                                    SPPictureLibrary addpics = (SPPictureLibrary)web.Lists[documentLibraryName];

                                    SPFile file = addpics.RootFolder.Files.Add((string)imageId + ".bmp", imageBytes, true);

                                    SPListItem item = file.Item;

                                    var createdtime= image.SelectToken("created_time");

                                   double mydoubleval = createdtime; // this line gives error


                                }
                            }

                            }

                        }

Open in new window



I get error as
 cannot convert from 'Newtonsoft.Json.Linq.JToken' to 'double'	

Open in new window


How to resolve this? I get error on line
                                   double mydoubleval = createdtime; // this line gives error

Open in new window

Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

createdtime sounds like a timestamp, so why a double?
Anyway, try with a specific convert:
double mydoubleval = Convert.ToDouble(createdtime);

Open in new window

/gustav
Avatar of louisfr
louisfr

http://www.newtonsoft.com/json/help/html/Operators_T_Newtonsoft_Json_Linq_JToken.htm
Conversions from JToken to other types are explicit.
Try casting:
double mydoubleval = (double)createdtime;

Open in new window

Avatar of kirannandedkar

ASKER

@louisfr i had tried that as well and i get error  
Can not convert String to Double.

Open in new window

@Gustav Brock i still get error after using convert.todouble
Then it probably isn't possible - at least using standard methods.

What does createdtime actually contain? As said, it sound like a timestamp and - if a string - you would have to convert that to DateTime first.

/gustav
ASKER CERTIFIED SOLUTION
Avatar of kirannandedkar
kirannandedkar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, as I suspected. Thanks for the feedback.

/gustav
I get unixtimestamp from instagram api. So used function UnixTimeStampToDateTime to convert to datetime.