0

My object it looks like that

{"Items":[{number:8468},{}],

 "count":2}

I need to take for example number of item 1

my code is that and it is not working

var content = await response.Content.ReadAsStringAsync();
var myObject = JsonConvert.DeserializeObject<ICollection<ViewModel>>(content);
1

2 Answers 2

1

The JSON here represents an object, not a collection - which is to say: the outer node is {}, not []. As such, create a type that reflects that:

public class Foo {
    public List<Bar> Items {get;} = new List<Bar>();
    [JsonProperty("count")]
    public int Count {get;set;}
}
public class Bar {
    [JsonProperty("number")]
    public int Number {get;set;}
}

and use:

var myObject = JsonConvert.DeserializeObject<Foo>(content);
Sign up to request clarification or add additional context in comments.

Comments

0

Here you have a json object not json array. So your logic is failing. So first create a model like this :

public class Response {

    public List<Item> Items {get;set;} = new List<Item>();
    public int Count {get;set;}
}

public class Item {
    public int Number {get;set;}
}

and replace :

var myObject = JsonConvert.DeserializeObject<ICollection<ViewModel>>(content);

with

var result = JsonConvert.DeserializeObject<Response>(content);

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.