0

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.

0

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is good. Thank you. But now I would need something like foreach(subarray in json["d"]["oddsdata"]["back"]["E-1-2-0-0-0"]), how could I do it?
@LukAss741 You can do this to loop through all the key/values in a child JObject foreach (JProperty prop in ((JObject)json["d"]["oddsdata"]["back"]["E-1-2-0-0-0"]).Properties()) { Console.WriteLine(string.Format("obj[{0}] = {1}", prop.Name, prop.Value)); } but as you can see, with such a poorly formatted JSON it's going to be a PITA
0

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.

5 Comments

What do you mean by parameter? The source is written like -|-{json}-|-, I don't know why. So That substring was used to trim it. Or could this somehow harm the json string?
Oh, so there's a string -|-{json}-|- appended to the front of the json string that is in the pastebin link?
I posted on pastebin its structured code to make it human readable. Original has "-|-" at the begining and end.
is your DataSet object corresponding to the json string format?
I don't get what you mean. I don't have too much experience with json and this is the first time I am trying to work with it in c#. If I had in example tis code: { "s": 1, "d": { "bt": 1, "sc": 2 } } Then I would like to acces it like: variable["d"]["bt"]; which would be equal 1. Is this way to approach it even correct?

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.