Antoine
Antoine

Reputation: 312

Strange JSON Deserialization c#

I'm having troubles with some Deserialization because I'm not receiving an object like I'm used to but an array. Lets to the code, how I've being doing it:

JSON received:

[{"gcm_regID":"fsdf2156fw62f1wfwe512g"}]

Deserialization

var result = JsonConvert.DeserializeObject<Gcm>(response);

Handlers Tried:

public class Gcm
{
    public List<Gcm_reg> gcm_regID { get; set; }
}
public class Gcm_reg
{
    public string gcm_regID { get; set; }
}

I've tried just the Gcm_reg as well and nothing seems to work. How can I deserialize this? By the way, I'm using the newsoft JSON.

Best regards,

Upvotes: 1

Views: 146

Answers (2)

Ashish Rajput
Ashish Rajput

Reputation: 1529

Object should be like

public class Gcm
{
    public string gcm_regID { get; set; }
}

So Try it

var result = JsonConvert.DeserializeObject<IEnumerable<Gcm>>(response);

You can directly generate classes from Json by using link

To create same type of object

var outputList = new List<Gcm>();

//loop through this part based on number of input
var itemToAdd = new Gcm();
itemToAdd .regID  = 'something';
outputList.Add(itemToAdd );

and send outputList to server.

Upvotes: 2

Pratap Bhaskar
Pratap Bhaskar

Reputation: 408

 IEnumerable<Gcm_reg> result =  JsonConvert.DeserializeObject<IEnumerable<Gcm_reg>>(response);

You can deserialize the object as an array or IEnumerable

Upvotes: 4

Related Questions