Convert an Listto user defined class
I have some data that is object IEnumerable, how to do I convert it into class type, the name of class is option
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
class Option
{
public long Id { get; set; }
public string Name { get; set; }
public long Price { get; set; }
public long Status { get; set; }
public long Archive { get; set; }
}
so far tried =>
foreach (var VARIABLE_3 in (IEnumerable) VARIABLE_2)
{
if ( VARIABLE_3.ToString().Contains("option") && !VARIABLE_3.ToString().Contains("options"))
{
object values = ((IEnumerable)VARIABLE_3).Cast<object>().ToArray();
//Option values = VARIABLE_3.ToString().Cast<Option>().ToArray();
foreach (var VARIABLE in values)
{
strStringBuilder.Append("VARIABLE: " + VARIABLE + "n");
}
}
}
c# winforms c#-5.0
|
show 6 more comments
I have some data that is object IEnumerable, how to do I convert it into class type, the name of class is option
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
class Option
{
public long Id { get; set; }
public string Name { get; set; }
public long Price { get; set; }
public long Status { get; set; }
public long Archive { get; set; }
}
so far tried =>
foreach (var VARIABLE_3 in (IEnumerable) VARIABLE_2)
{
if ( VARIABLE_3.ToString().Contains("option") && !VARIABLE_3.ToString().Contains("options"))
{
object values = ((IEnumerable)VARIABLE_3).Cast<object>().ToArray();
//Option values = VARIABLE_3.ToString().Cast<Option>().ToArray();
foreach (var VARIABLE in values)
{
strStringBuilder.Append("VARIABLE: " + VARIABLE + "n");
}
}
}
c# winforms c#-5.0
1
What's the issue you are facing with this code? What is the datatype ofVARIABLE_2
?
– Chetan Ranpariya
Jan 2 at 11:55
The issue would be that it just produces a StringBuilder.
– bommelding
Jan 2 at 11:58
List<object> is the data type of VARIABLE_2
– Developer
Jan 2 at 11:58
1
"data that is object IEnumerable" how, where from? What you post is JSon andJson.Deserialize()
couldbe your best option.
– bommelding
Jan 2 at 11:59
maybe you are looking for something like this: foreach(var VARIABLE_3 in (VARIABLE_2 as IEnumerable<Option>))...
– Stefan W.
Jan 2 at 12:00
|
show 6 more comments
I have some data that is object IEnumerable, how to do I convert it into class type, the name of class is option
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
class Option
{
public long Id { get; set; }
public string Name { get; set; }
public long Price { get; set; }
public long Status { get; set; }
public long Archive { get; set; }
}
so far tried =>
foreach (var VARIABLE_3 in (IEnumerable) VARIABLE_2)
{
if ( VARIABLE_3.ToString().Contains("option") && !VARIABLE_3.ToString().Contains("options"))
{
object values = ((IEnumerable)VARIABLE_3).Cast<object>().ToArray();
//Option values = VARIABLE_3.ToString().Cast<Option>().ToArray();
foreach (var VARIABLE in values)
{
strStringBuilder.Append("VARIABLE: " + VARIABLE + "n");
}
}
}
c# winforms c#-5.0
I have some data that is object IEnumerable, how to do I convert it into class type, the name of class is option
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
"option": {
"id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
}
class Option
{
public long Id { get; set; }
public string Name { get; set; }
public long Price { get; set; }
public long Status { get; set; }
public long Archive { get; set; }
}
so far tried =>
foreach (var VARIABLE_3 in (IEnumerable) VARIABLE_2)
{
if ( VARIABLE_3.ToString().Contains("option") && !VARIABLE_3.ToString().Contains("options"))
{
object values = ((IEnumerable)VARIABLE_3).Cast<object>().ToArray();
//Option values = VARIABLE_3.ToString().Cast<Option>().ToArray();
foreach (var VARIABLE in values)
{
strStringBuilder.Append("VARIABLE: " + VARIABLE + "n");
}
}
}
c# winforms c#-5.0
c# winforms c#-5.0
edited Jan 2 at 12:49
Developer
asked Jan 2 at 11:53
DeveloperDeveloper
1,37492849
1,37492849
1
What's the issue you are facing with this code? What is the datatype ofVARIABLE_2
?
– Chetan Ranpariya
Jan 2 at 11:55
The issue would be that it just produces a StringBuilder.
– bommelding
Jan 2 at 11:58
List<object> is the data type of VARIABLE_2
– Developer
Jan 2 at 11:58
1
"data that is object IEnumerable" how, where from? What you post is JSon andJson.Deserialize()
couldbe your best option.
– bommelding
Jan 2 at 11:59
maybe you are looking for something like this: foreach(var VARIABLE_3 in (VARIABLE_2 as IEnumerable<Option>))...
– Stefan W.
Jan 2 at 12:00
|
show 6 more comments
1
What's the issue you are facing with this code? What is the datatype ofVARIABLE_2
?
– Chetan Ranpariya
Jan 2 at 11:55
The issue would be that it just produces a StringBuilder.
– bommelding
Jan 2 at 11:58
List<object> is the data type of VARIABLE_2
– Developer
Jan 2 at 11:58
1
"data that is object IEnumerable" how, where from? What you post is JSon andJson.Deserialize()
couldbe your best option.
– bommelding
Jan 2 at 11:59
maybe you are looking for something like this: foreach(var VARIABLE_3 in (VARIABLE_2 as IEnumerable<Option>))...
– Stefan W.
Jan 2 at 12:00
1
1
What's the issue you are facing with this code? What is the datatype of
VARIABLE_2
?– Chetan Ranpariya
Jan 2 at 11:55
What's the issue you are facing with this code? What is the datatype of
VARIABLE_2
?– Chetan Ranpariya
Jan 2 at 11:55
The issue would be that it just produces a StringBuilder.
– bommelding
Jan 2 at 11:58
The issue would be that it just produces a StringBuilder.
– bommelding
Jan 2 at 11:58
List<object> is the data type of VARIABLE_2
– Developer
Jan 2 at 11:58
List<object> is the data type of VARIABLE_2
– Developer
Jan 2 at 11:58
1
1
"data that is object IEnumerable" how, where from? What you post is JSon and
Json.Deserialize()
couldbe your best option.– bommelding
Jan 2 at 11:59
"data that is object IEnumerable" how, where from? What you post is JSon and
Json.Deserialize()
couldbe your best option.– bommelding
Jan 2 at 11:59
maybe you are looking for something like this: foreach(var VARIABLE_3 in (VARIABLE_2 as IEnumerable<Option>))...
– Stefan W.
Jan 2 at 12:00
maybe you are looking for something like this: foreach(var VARIABLE_3 in (VARIABLE_2 as IEnumerable<Option>))...
– Stefan W.
Jan 2 at 12:00
|
show 6 more comments
1 Answer
1
active
oldest
votes
Use JavascriptSerializer
or Json.Net
serializer to serialize or deserialize Json string.
string jsonArrayObject = @"
[
{ "id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
},
{
"id": 8208,
"name": "Coke",
"price": 0.0,
"status": 1,
"archive": 0
}
]";
Javascript Serializer
List<Option> optionList = new System.Web.Script.Serialization.
JavaScriptSerializer().Deserialize<List<Option>>(jsonArrayObject);
Json.Net
List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(jsonArrayObject);
this is in variable_3 List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(VARIABLE_3.ToString());
– Developer
Jan 2 at 12:50
throws an exception of "Could not cast or convert from System.String to System.Collections.Generic.List`1[some.Library.Option]."
– Developer
Jan 2 at 12:51
Do not useVARIABLE_3.Tostring()
use that string variable which contains whole json string as I created in examplestring jsonArrayObject
– Umair Anwaar
Jan 2 at 12:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005878%2fconvert-an-listobjectto-user-defined-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use JavascriptSerializer
or Json.Net
serializer to serialize or deserialize Json string.
string jsonArrayObject = @"
[
{ "id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
},
{
"id": 8208,
"name": "Coke",
"price": 0.0,
"status": 1,
"archive": 0
}
]";
Javascript Serializer
List<Option> optionList = new System.Web.Script.Serialization.
JavaScriptSerializer().Deserialize<List<Option>>(jsonArrayObject);
Json.Net
List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(jsonArrayObject);
this is in variable_3 List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(VARIABLE_3.ToString());
– Developer
Jan 2 at 12:50
throws an exception of "Could not cast or convert from System.String to System.Collections.Generic.List`1[some.Library.Option]."
– Developer
Jan 2 at 12:51
Do not useVARIABLE_3.Tostring()
use that string variable which contains whole json string as I created in examplestring jsonArrayObject
– Umair Anwaar
Jan 2 at 12:57
add a comment |
Use JavascriptSerializer
or Json.Net
serializer to serialize or deserialize Json string.
string jsonArrayObject = @"
[
{ "id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
},
{
"id": 8208,
"name": "Coke",
"price": 0.0,
"status": 1,
"archive": 0
}
]";
Javascript Serializer
List<Option> optionList = new System.Web.Script.Serialization.
JavaScriptSerializer().Deserialize<List<Option>>(jsonArrayObject);
Json.Net
List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(jsonArrayObject);
this is in variable_3 List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(VARIABLE_3.ToString());
– Developer
Jan 2 at 12:50
throws an exception of "Could not cast or convert from System.String to System.Collections.Generic.List`1[some.Library.Option]."
– Developer
Jan 2 at 12:51
Do not useVARIABLE_3.Tostring()
use that string variable which contains whole json string as I created in examplestring jsonArrayObject
– Umair Anwaar
Jan 2 at 12:57
add a comment |
Use JavascriptSerializer
or Json.Net
serializer to serialize or deserialize Json string.
string jsonArrayObject = @"
[
{ "id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
},
{
"id": 8208,
"name": "Coke",
"price": 0.0,
"status": 1,
"archive": 0
}
]";
Javascript Serializer
List<Option> optionList = new System.Web.Script.Serialization.
JavaScriptSerializer().Deserialize<List<Option>>(jsonArrayObject);
Json.Net
List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(jsonArrayObject);
Use JavascriptSerializer
or Json.Net
serializer to serialize or deserialize Json string.
string jsonArrayObject = @"
[
{ "id": 8204,
"name": "250 ML",
"price": 40.0,
"status": 1,
"archive": 0
},
{
"id": 8208,
"name": "Coke",
"price": 0.0,
"status": 1,
"archive": 0
}
]";
Javascript Serializer
List<Option> optionList = new System.Web.Script.Serialization.
JavaScriptSerializer().Deserialize<List<Option>>(jsonArrayObject);
Json.Net
List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(jsonArrayObject);
answered Jan 2 at 12:35
Umair AnwaarUmair Anwaar
492519
492519
this is in variable_3 List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(VARIABLE_3.ToString());
– Developer
Jan 2 at 12:50
throws an exception of "Could not cast or convert from System.String to System.Collections.Generic.List`1[some.Library.Option]."
– Developer
Jan 2 at 12:51
Do not useVARIABLE_3.Tostring()
use that string variable which contains whole json string as I created in examplestring jsonArrayObject
– Umair Anwaar
Jan 2 at 12:57
add a comment |
this is in variable_3 List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(VARIABLE_3.ToString());
– Developer
Jan 2 at 12:50
throws an exception of "Could not cast or convert from System.String to System.Collections.Generic.List`1[some.Library.Option]."
– Developer
Jan 2 at 12:51
Do not useVARIABLE_3.Tostring()
use that string variable which contains whole json string as I created in examplestring jsonArrayObject
– Umair Anwaar
Jan 2 at 12:57
this is in variable_3 List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(VARIABLE_3.ToString());
– Developer
Jan 2 at 12:50
this is in variable_3 List<Option> optionList = JsonConvert.DeserializeObject<List<Option>>(VARIABLE_3.ToString());
– Developer
Jan 2 at 12:50
throws an exception of "Could not cast or convert from System.String to System.Collections.Generic.List`1[some.Library.Option]."
– Developer
Jan 2 at 12:51
throws an exception of "Could not cast or convert from System.String to System.Collections.Generic.List`1[some.Library.Option]."
– Developer
Jan 2 at 12:51
Do not use
VARIABLE_3.Tostring()
use that string variable which contains whole json string as I created in example string jsonArrayObject
– Umair Anwaar
Jan 2 at 12:57
Do not use
VARIABLE_3.Tostring()
use that string variable which contains whole json string as I created in example string jsonArrayObject
– Umair Anwaar
Jan 2 at 12:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005878%2fconvert-an-listobjectto-user-defined-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What's the issue you are facing with this code? What is the datatype of
VARIABLE_2
?– Chetan Ranpariya
Jan 2 at 11:55
The issue would be that it just produces a StringBuilder.
– bommelding
Jan 2 at 11:58
List<object> is the data type of VARIABLE_2
– Developer
Jan 2 at 11:58
1
"data that is object IEnumerable" how, where from? What you post is JSon and
Json.Deserialize()
couldbe your best option.– bommelding
Jan 2 at 11:59
maybe you are looking for something like this: foreach(var VARIABLE_3 in (VARIABLE_2 as IEnumerable<Option>))...
– Stefan W.
Jan 2 at 12:00