• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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

0
kirannandedkar
Asked:
kirannandedkar
  • 4
  • 3
1 Solution
 
Gustav BrockCIOCommented:
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
0
 
louisfrCommented:
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

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

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
kirannandedkarAuthor Commented:
@Gustav Brock i still get error after using convert.todouble
0
 
Gustav BrockCIOCommented:
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
0
 
kirannandedkarAuthor Commented:
I managed to solve the problem

i used below

  var createdTime = image.SelectToken("created_time").Value<double>();
 double mydoubleval = imageCreatedTime;
 DateTime imgCreatedTime = UnixTimeStampToDateTime(mydoubleval);


   public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
        {
            // Unix timestamp is seconds past epoch
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
            return dtDateTime;
        }
            

Open in new window

0
 
Gustav BrockCIOCommented:
OK, as I suspected. Thanks for the feedback.

/gustav
0
 
kirannandedkarAuthor Commented:
I get unixtimestamp from instagram api. So used function UnixTimeStampToDateTime to convert to datetime.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now