简体   繁体   中英

Deserializing Json to an Array type Propoerty

I am new to Json. I am trying to populate my object 'Sources' from json string with:

public class Sources  
{
   public long[] SourceIds;
}


string jsonstring = " [\r\n {\r\n  \"SourceIds\": 20181234\r\n  }, \r\n 
{\r\n  \"SourceIds\": 20181234\r\n  },\r\n {\r\n  \"SourceIds\": 
20181234\r\n  }\r\n]"

My aim is to have an object of 'Sources' with the values of 'SourceIds' populated.

Thanks in advance,

I suspect that your actual Json is like below:

string jsonstring = " [
 {
      \"SourceIds\": 20181234
  },  
  {
      \"SourceIds\": 20181234
  },
 {
      \"SourceIds\": 20181234
  }
]"

You can serialize your json with

var result = JsonConvert.DeserializeObject<IList<Class1>>(String)

Serialization Classes

public class Class1
{
    public int SourceIds { get; set; }
}

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM