I have an Json string which is receiveCount({\\"url\\":\\"http://www.google.com\\",\\"count\\":75108})
My complete Method is
public void GetPinCount(string url)
{
string QUrl = "https://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url=" + url;
System.Net.HttpWebRequest Request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(QUrl);
Request.ContentType = "text/json";
Request.Timeout = 10000;
Request.Method = "GET";
string content;
using (WebResponse myResponse = Request.GetResponse())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8))
{
content = sr.ReadToEnd();
}
};
var json = JObject.Parse(content);
var share_count = json["receiveCount"]["count"].ToString();
Console.WriteLine("Share Count :" + share_count);
}
When I am trying to access the count i am getting an exception like
Unexpected character encountered while parsing value: r. Path '', line 0, position 0.
Please tell me how this can be done.
Your string is not valid JSON :
receiveCount(`{\"url\":\"http://www.google.com\",\"count\":75108}`)
The valid JSON part is the parameter :
{"url":"http://www.google.com","count":75108}
You must extract the valid JSON part from your string to deserialize it.
You are calling the wrong property.
You should use
var share_count = json["count"].ToString();
instead of,
var share_count = json["receiveCount"]["count"].ToString();
Code to use with response:
var json = JObject.Parse (content);
var share_url = json["url"];
Console.WriteLine ("Share URL:" + share_url);
var share_count = json["count"];
Console.WriteLine ("Share Count:" + share_count);
EDIT 1:
// Gets only the JSON string we need
content = content.Replace ("receiveCount(", "");
content = content.Remove (content.Length - 1);
// Converts JSON string to Object
var json = JObject.Parse (content);
var share_url = json["url"];
Console.WriteLine ("Share URL:" + share_url);
var share_count = json["count"];
Console.WriteLine ("Share Count:" + share_count);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.