0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried extracting it but still getting the same error
What did you extract ? What is the exact error ? If you extract the JSON string you cannot get an error saying that the string starts with the character 'r'.
0

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

4 Comments

first of all, i am unable to parse it to json.. then only i can think about getting data from it.
Seems the url (eg: api.pinterest.com/v1/urls/…) returns json string inside receiveCount method. So as a workaround I removed unwanted text from the response. Check if it works. :)
And FYI, there is no MIME type "text/json", "application/json" might be appropriate. (Source: developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/…)
It works when i removed receivedcount from the text and then i tried to parse the string to json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.