Tech
Forums
Jobs
Books
Events
Live
Learn
More
Training
Interviews
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
2
Reply
Populate ComboBox from JSON Object
Salman Mushtaq
Dec 7 2017 3:28 AM
133
Reply
Hello, I have below Json
[
{
"customerid": 1,
"name": "ABS",
"email": "true",
"reference": "2016-03-21T10:44:32+03:00",
"status": 0,
"dateupdated": "2017-12-07T04:56:05.583"
},
{
"customerid": 2,
"name": "BMS",
"email": "true",
"reference": "2016-03-13T19:17:54+03:00",
"status": 0,
"dateupdated": "2017-12-07T04:56:05.757"
},
{
"customerid": 3,
"name": "XYA",
"email": "true",
"reference": "2016-03-13T19:17:54+03:00",
"status": 0,
"dateupdated": "2017-12-07T04:56:05.757"
}
]
In my windows form I create class API.cs
class
API
{
}
public
class
RootObject
{
public
int
customerid {
get
;
set
; }
public
string
name {
get
;
set
; }
public
string
email {
get
;
set
; }
public
DateTime reference {
get
;
set
; }
public
int
status {
get
;
set
; }
public
DateTime dateupdated {
get
;
set
; }
}
I need to populate combo box customer_cb with "name". I use DataContractJsonSerializer
WebClient client =
new
WebClient();
string
json = client.DownloadString(url);
using
(var ms =
new
MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer deserializer =
new
DataContractJsonSerializer(
typeof
(RootObject));
RootObject obj = (RootObject)deserializer.ReadObject(ms);
// Assign data to combo box
foreach
(var name
in
obj.name)
{
ComboboxItem item =
new
ComboboxItem();
item.Text = name.ToString();
customer_cb.Items.Add(item);
}
}
When I run the application I get error
Object Reference not
set
to an instance of an
object
Please help.
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
how to show location in google map dynamic mvc
textarea content should affect the database