0

I am using Oauth2 to get information about a user from Facebook. The result I get is in a JSON format, but when I try to parse it using JSON.NET I get an error saying: Exception : System.ArgumentException: Incomplete JSON input. At line 1, column 0.

Sample code:

request.GetResponseAsync().ContinueWith(t =>{
if(!t.IsFaulted && !t.IsCanceled){
    try{
        Console.WriteLine(t.Result.GetResponseText());

        dynamic obj = JsonObject.Parse(t.Result.GetResponseText());
        Console.WriteLine(obj.first_name);

    } catch (Exception e) {
        Console.WriteLine("Exception : "+ e);
    }
  }
});

The t.Result.GetResponseText() method returns a string in this format:

{
"id":"some_id",
"bio":"some_bio",
"first_name":"some_name",
"gender":"male",
"last_name":"some_name",
"link":"https:\/\/www.facebook.com\/app_scoped_user_id\/some_user_id\/",
"locale":"nb_NO",
"middle_name":"some_name",
"name":"some_name",
"timezone":1,
"updated_time":"2014-09-18T12:48:34+0000",
"verified":true
}

Are someone able to point out what I am missing here? Thanks in advance!

2
  • Have you debugged and that's what you're getting? JSON seems to be valid... Commented Feb 5, 2015 at 12:15
  • This is the error message. I also think that the JSON seems to be valid. So i dont understand. Exception : System.ArgumentException: Incomplete JSON input. At line 1, column 0 at System.Runtime.Serialization.Json.JavaScriptReader.ReadCore () [0x00225] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.0.51/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ Commented Feb 5, 2015 at 12:19

3 Answers 3

2

Create a class which can deserialize the given json data

public class Rootobject
        {
            public string id { get; set; }
            public string bio { get; set; }
            public string first_name { get; set; }
            public string gender { get; set; }
            public string last_name { get; set; }
            public string link { get; set; }
            public string locale { get; set; }
            public string middle_name { get; set; }
            public string name { get; set; }
            public int timezone { get; set; }
            public DateTime updated_time { get; set; }
            public bool verified { get; set; }
        }

then deserialize the json data using JSON.NET

var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(t.Result.GetResponseText());

Worked for me
Hope this helps

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

2 Comments

I no longer get the JSON error message, but if I try to output anything from the data-object like Console.WriteLine(data.id), I get a NullReferenceException. Do you get it to?
Edit: Strange. If I copy the JSON string in a string variable and then use it in the deserialize-method, the data-object is not null. If i use the t.Result.GetResponseText() the data-object is null.
2

The error message indicates that the response text is an empty string (incomplete JSON input, first line, first character).

Just a hunch: does the problem resolve itself if you store the response text in a temporary variable?

var responseText = t.Result.GetResponseText();
Console.WriteLine(responseText);
dynamic obj = JsonObject.Parse(responseText);

The reason why this works is that t.Result.GetResponseText() only returns a value the first time that you call it. The underlying stream does not support seeking, and the JSON reader does not cache the value internally.

5 Comments

No it does not, however if I copy the JSON string in a string variable and then use it in the deserialize-method, the data-object is not null. If i use the t.Result.GetResponseText() the data-object is null. if i i store the response text in a temporary variable the data-object is also null.
So clearly the return value of t.Result.GetResponseText() is not the same string as the one that you are pasting in manually.
True, but then again I find it weird that when I output t.Result.GetResponseText() I get the exact JSON-string which I wrote in the original post.
My first thought was that GetResponseText() only returns a value the first time that you call it.
Your thought was correct, it does only return a value the first time you call it. Thank you!
0

Thanks to Steven Liekens we found out that the t.Result.GetResponseText()method only returns something the first time its called. The second time it returns an empty string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.