c#


How to use an array in json.net


How would I pass an array into JsonConvert.SerializeObject? I have the following working JSON but am unable to get it into the serialize function because it needs an array.
"recipients": [
{
"address": "a#a.com"
},
{
"address": "a#a.net"
},
{
"address": "a#gmail.com"
}
]
I'm new to c# and any help would be great, thanks!
C# body:
recipients = new Array {
}
Great way to create c# classes from JSON is http://json2csharp.com/. When I wrapped your JSON into curly braces as #dbc suggested and pasted there following code was generated:
public class Recipient
{
public string address { get; set; }
}
public class RootObject
{
public List<Recipient> recipients { get; set; }
}
Now you can deserialize it like this:
RootObject myObject = JsonConvert.DeserializeObject<RootObject>(myJSON);
In order to build a valid Json object, Please refer to http://json.org/
For below model,
public class Model
{
public string[] recipients { get; set; }
}
with below JsonDocument,
string jsondoc = "{\"recipients\": [\"a#a.com\",\"a#a.net\",\"a#gmail.com\"]}"
Below piece of code shoule be able to deserialize your json document
Model obj = new Model();
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsondoc)))
{
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Model));
obj = (Model)deserializer.ReadObject(ms);
}

Related Links

Test subscription to EventAggregator
DateFormatString not works after upgrade version NewtonSoft Json
How to read from console into String variable in C#
Sprite won't render at all?
Separation Steering Algorithm
Null reference exception in Xamarin iOS app
heavy Weight SQL inserts (SqlBulkCopy alternative) without the use of Stored procedure
How does Chrome and other apps override the default windows forms borders?
Dynamically change the default Text in a textbox
hresult from IMbnInterfaceManager::GetInterfaces when no MBN device exists
What does “IsDlg=1” means? - SharePoint Site
How to Add row in gridview with textbox
Information about the domain could not be retrieved (1355)
How to get the configuration property name out of a ConfigurationElementCollection
how to load from file to my App like blender file
Display outlook ribbons at runtime

Categories

HOME
reactjs
drupal
http
skype-for-business
ssh
customization
intellisense
popover
bigtable
boxplot
cryengine
nltk
atlassian-fisheye
collision-detection
country-codes
unreal-engine4
mongodb-replica-set
opendaylight
popup
dat.gui
dompdf
ibatis
draw2d
project-structure
infinite-loop
jira-zephyr
akavache
recreate
ios9
ibm-bpm
eclipse-jdt
availability
microstrategy
n1ql
javax
powerbi-embedded
kepserverex
queryover
motion
linkurious
file-conversion
office-interop
server-side-swift
ssdp
wicket-tester
delphi-xe
php-mongodb
binary-decision-diagram
qt-quick
nshttpurlresponse
api-key
taleo
azure-data-catalog
reqif
anonymous-function
ng-class
dcom
xamarin-test-cloud
androiddesignsupport
papyrus
jira-agile
scriptengine
key-value-store
periodic-processing
factorization
jquery-draggable
matlab-engine
recode
mojolicious
persian
nmock
pdcurses
valdr-bean-validation
gmaps4jsf
accpac
mathics
applinks
static-files
cocos2d-x-2.x
behance-api
traceur
email-headers
pthreads-win32
autofilter
randomaccessfile
misfire-instruction
ie-developer-tools
ojb
usersettings
symbian3
architectural-patterns
blotter
idoc
invalidoperationexception
pdf-reader
advanceddatagrid
helicontech
cgaffinetransform
virtual-printer
xpolog
overlays
gmagick
qvariant
meego-harmattan
response-time
quazip
onunload
xslt-grouping
helios
manuals

Resources

Encrypt Message