LukAss741
LukAss741

Reputation: 831

json.net DeserializeObject

I have following code:

string code = client.DownloadString("http://oddsportal.com/feed/prematch/1-1-hSpbs4Cd-1-2.dat");
DataSet data = JsonConvert.DeserializeObject<DataSet>(code.Substring(3, code.Length - 6));
textBox1.Text += "1";

But it stops on the second line like if there was return - it doesn't write 1 in the textbox. What am I doing wrong?

Here it is rewritten with structure: http://pastebin.com/xZAhjU8w Thanks.

EDIT: A used a try-catch and its exception is:

Newtonsoft.Json.JsonSerializationException: Additional text found in JSON string after finishing deserializing object. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) in c:\Temp\Json\Working\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 177 at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in c:\Temp\Json\Working\Newtonsoft.Json\Src\Newtonsoft.Json\JsonSerializer.cs:line 711 at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) in c:\Temp\Json\Working\Newtonsoft.Json\Src\Newtonsoft.Json\JsonSerializer.cs:line 663 at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) in c:\Temp\Json\Working\Newtonsoft.Json\Src\Newtonsoft.Json\JsonConvert.cs:line 797 at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) in c:\Temp\Json\Working\Newtonsoft.Json\Src\Newtonsoft.Json\JsonConvert.cs:line 757 at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) in c:\Temp\Json\Working\Newtonsoft.Json\Src\Newtonsoft.Json\JsonConvert.cs:line 694 at oddsreader.Form1.Form1_Load(Object sender, EventArgs e) in c:\Users\zuz\Documents\Visual Studio 2012\Projects\oddsreader\oddsreader\Form1.cs:line 692

When I tried some online json validators, they returned the string was valid. What could be wrong with it?

EDIT2: I might have been completely wrong. So I will ask a simpler quastion. How could I do a foreach of "odds" array and then foreach of its subarrays? The path to odds is: ["d"]["oddsdata"]["back"]["E-1-2-0-0-0"]["odds"]. I haven't found an example code with mode than 2 level arrays.

Upvotes: 0

Views: 11699

Answers (2)

Fred Deschenes
Fred Deschenes

Reputation: 71

Your issue isn't with the string, it's with the cast to 'DataSet'. This works fine for me :

static void Main(string[] args)
{
    WebClient client = new WebClient();
    string code = client.DownloadString("http://oddsportal.com/feed/prematch/1-1-hSpbs4Cd-1-2.dat");
    client.Dispose();

    code = code.Replace("-|-", string.Empty);

    JObject json = JsonConvert.DeserializeObject<JObject>(code);

    int one = (int)json["d"]["bt"];

    Debug.Assert(one == 1);
}

Upvotes: 1

ali
ali

Reputation: 881

It seems like JsonConvert could not execute DeserializeObject on your parameter that you passed in.

That is to say (3, code.Length - 6) probably does not give you a valid json string, hence JsonConvert cannot convert it.

An exception was probably thrown but you did not catch it, hence, it exhibited the "return" like behavior.

Upvotes: 0

Related Questions