1

Very new to C# and JSON. I have been struggling with this problem for about a day and can't figure it out.

JSONLint states both JSON strings are valid.

Trying to deserialize the following

{"items":[{"id":"1122267","quantity":"1","bundle":"1"}],"seconds_ago":"1"}

throws the exception

An unhandled exception of type 'System.OutOfMemoryException' occurred in Newtonsoft.Json.dll

If I try

{"items":[{"id":"1122267","quantity":"1","bundle":"1"}]} 

it works.

I'm reading the JSON string from a textbox and then deserializing using the following

string json = textBox1.Text;
DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(json);
2
  • Are you sure you are using the latest version of JSON.NET? Sounds like it might be a bug. Commented Jun 11, 2014 at 2:09
  • 1
    Does {"seconds_ago":"1", "items":[{"id":"1122267","quantity":"1","bundle":"1"}]} work? And what about just {"seconds_ago":"1"}? Commented Jun 11, 2014 at 2:12

2 Answers 2

4

I'm not at all familiar with DataSet, but after playing around with it a bit, I think I've found the reason. According to the documentation:

The DataSet consists of a collection of DataTable objects

Because a DataSet is a collection of objects, you can only deserialize a collection (array) into it. So,

{"items":[{"id":"1122267","quantity":"1","bundle":"1"}],"seconds_ago":"1"}

will not work, because it contains one collection (of items) and one property (seconds_ago). On the other hand,

{"items":[{"id":"1122267","quantity":"1","bundle":"1"}]} 

works, because it only contains one collection.

Therefore, if you really want to deserialize your json string into a DataSet, you should do this instead:

{
    "objects": [
        {
            "items":[{"id":"1122267","quantity":"1","bundle":"1"}],
            "seconds_ago":"1"
        }
    ]
} 

You should really consider deserializing into a C# object though, which in my opinion is less complicated and easier to handle:

public class RootObject
{
    public List<Item> items { get; set; }
    public string seconds_ago { get; set; }
}

public class Item
{
    public string id { get; set; }
    public string quantity { get; set; }
    public string bundle { get; set; }
}

RootObject deserializedObject = JsonConvert.DeserializeObject<RootObject>(json);
Sign up to request clarification or add additional context in comments.

7 Comments

Deserializing into a C# object seems to work. But im having a tough time looking at the contents. For example Console.WriteLine(RootObject.seconds_ago); fails. Idealy i would like to show the data in a datagridview. Using my original method i could just bind the datatable to the datagridview using dataGridView1.DataSource = dataTable;. I assume i will have to loop and write to the grid? Any examples would be most useful. Thanks
@Tiago How does it fail? Did your original json string contain a value for seconds_ago? If seconds_ago is a string and your json data did not contain such a value, RootObject.seconds_ago will return null.
I receive 'Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.RootObject.seconds_ago.get'`. I'm using line 13 from [james.newtonking.com/json/help/index.html?topic=html/… just suited to my example. I have tried other variations which produce the same error above. Thanks
@Tiago You are trying to access seconds_ago from the class RootObject. That is wrong. You should access it with the initialized object/instance of the class (deserializedObject in my example). Look closely at the documentation from JSON.NET. It uses account.Email, which is an instance of the Account class, and not Account.Email. Note the case. You probably should get started with a C# tutorial/walkthrough, such as this one, before going any further. It will help immensely :)
@Tiago As for binding an object to a DataGridView, that is an entirely different question. You may refer to Populating DataGridView with list of objects and How does one bind a complex object to a DataGridView?. There's also an official walkthrough you could try. If those don't help, you could ask a new question.
|
-1

The DataSet class does not have a property seconds_ago or items. So therefore

DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(json);

Will never work because you cannot convert that particular json string to a DataSet

1 Comment

You're assuming he's using the built in DataSet.

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.