0

I'm having troubles to desirialize a JSON string to n object using JSON.net

I've tried the following but results are always null :

class auctionList
{
    public auctionInfo auctionInfo { get; set; }
}

class auctionInfo
{
    public IList<auction> auctions { get; set; }
}

class auction
{
    public string tradeId { get; set; }
}

Here is the json string :

{
"auctionInfo": [
    {
        "tradeId": 276649263881
    },
    {
        "tradeId": 356444585498
    },
    .......
    ]
  }


auctionList auctions = JsonConvert.DeserializeObject<auctionList>(json);

auctions

is always null.

and is DeserializeObject the fastest way to do it if I just wanna reach 'tradeId' ?

Any ideas ? Thanks.

4
  • 1
    Your json doesn’t match your class structure. auctioninfo is an object in your class whereas in your json it is an array. Commented Apr 10, 2020 at 12:10
  • 1
    Try the reverse: In code, make an auctionList, populated with your data (shown above) and serialize it to json. Compare it to your incoming data. Commented Apr 10, 2020 at 12:25
  • @weichch it was it ! Commented Apr 10, 2020 at 12:27
  • @tgolisch thanks for the advice ! Will do it next time i'm working with json ! Commented Apr 10, 2020 at 12:27

1 Answer 1

1

You need modify your string to:

{
"auctionInfo": { "auctions":
    [
    {
        "tradeId": 276649263881
    },
    {
        "tradeId": 356444585498
    }
    ]
    }
  }

or set your classes to:

public class auction
{
    public Auctioninfo[] auctionInfo { get; set; }
}

public class Auctioninfo
{
    public string tradeId { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

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.