Convert an Listto user defined class












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");
}
}
}









share|improve this question




















  • 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
















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");
}
}
}









share|improve this question




















  • 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














0












0








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");
}
}
}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












1 Answer
1






active

oldest

votes


















2














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);





share|improve this answer
























  • 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 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













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
});


}
});














draft saved

draft discarded


















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









2














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);





share|improve this answer
























  • 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 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


















2














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);





share|improve this answer
























  • 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 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
















2












2








2







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);





share|improve this answer













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);






share|improve this answer












share|improve this answer



share|improve this answer










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 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





















  • 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 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



















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






















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules