5
Hello, I have an entire JSON inside a string and I need to turn it into an object in order to access some data... I’ll leave the code below to see if you can help me.
My JSON is in that string:
string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
part of the JSON:
{
"?xml":{"@version":"1.0","@encoding":"Windows-1252","@standalone":"yes"},
"VFPData": {
"row": [
{
"@numero": "000",
"@descricao": "Menu principal",
},
{
"@numero": "001",
"@descricao": "Arquivos",
}
]
}
}
I need to access the values of these @descrição
.
I’m using .Net Core Razor Pages... someone there could give me a hand with this ???
JSON(JSON Visualize from VS):
the @descricao
is the @des_mn0
, that I need to take.
AS I AM DOING:
public class DadosMenu
{
[JsonProperty("VFPData")]
public VFP vfp { get; set; }
}
public class VFP
{
[JsonProperty("row")]
public Row[] row { get; set; }
}
public class Row
{
[JsonProperty("@des_mn0")]
public string des { get; set; }
}
(public IActionResult OnGet(){}
):
var xml = System.IO.File.ReadAllText(@"C:\Dados\VS\MenuXML\menuV11.Xml");
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc).ToString();
DadosMenu result = JsonConvert.DeserializeObject<DadosMenu>(json);
for (int i = 1; i < 9; i++)
{
descr += result.vfp.row[i].des + " - ";
}
HttpContext.Session.SetString("xmltojson", descr);
EDIT: Okay, I’m already getting the values I need... thank you all for the answers, and I’ve updated the question, showing how I’m doing to get the values.
If possible, if you can do the same (take a specific value) in an XML, it will save me a little work... then I will leave part of the XML below, so you can know how to do...
OBS:
1 - XML refers to the same json subject. I just transformed it.
2 - I need to save this data I took, in a lista
, so that I can then use them to assemble a menu.(nav-bar
)
(XML)
<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>
<VFPData>
<xsd:schema id="VFPData" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="VFPData" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="row" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="num_mn0" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="15"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="des_mn0" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="200"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:choice>
<xsd:anyAttribute namespace="http://www.w3.org/XML/1998/namespace" processContents="lax"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<row num_mn0="000" des_mn0="Menu principal"/>
<row num_mn0="001" des_mn0="Arquivos"/>
I need to get the value of des_mn0
.
EDIT2:
error that appears when trying to answer Alisson:
Onget:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
DadosMenu result = JsonConvert.DeserializeObject<DadosMenu>(json);
ViewData["DadosMenu"] = result.vfp.row;
return Page();
I had, yesterday, created a list that I keep all the information I need... I just don’t know how to get this data in HTML, look how I did:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
DadosMenu result = JsonConvert.DeserializeObject<DadosMenu>(json);
List<string> MenuPrincipal = new List<string>();
int i = 1;
while (result.vfp.row[i].men != "")
{
if (result.vfp.row[i].men == "_MSYSMENU")
{
MenuPrincipal.Add(result.vfp.row[i].des);
}
else
{
break;
}
i++;
}
ViewData["MenuPrincipal"] = MenuPrincipal;
return Page();
The data you receive is json itself?
– novic
is yes @Virgilionovic... I’m actually getting in xml and turn to json, as the code shows
– user149429
if you are receiving in XML it was not better to do for it?
– novic
@Virgilionovic, if you have any idea how to do it, you can leave it in the answers... but I tried it the way Alisson commented, but it didn’t work out... if you want, tell me that I leave part of the xml in the question.
– user149429
Samuel I made a version with Json, if put the XML later I do with it also just post the question and adapt to that because you asked with JSON.
– novic
quiet... in about 30 Minutes I make the changes for you @Virgilionovic
– user149429
I think it’s fitting @Virgilionovic
– user149429
@Samuelmumbered by what I saw in the code you have now, with the code you already have, change in your VFP class the Row[] by List<Row>. Then in your result you will already have the list ready, just use result.vfp.Row to access.
– Alisson Marqui
I’ll take the test, but thanks for the @Alissonmarqui tip. So I should exchange, within my VFP class, the
public Row[] row { get; set; }
forpublic List<Row> row { get; set; }
?– user149429
@Samuelmed this, by making this switch the Row property of the VFP class will be a list of the Row object, and Jsonconvert already Convert the JSON in List.
– Alisson Marqui
@Samuelmachado If I understand, you want to put this on a list to return to your View as Model, right ? if it is I edit my reply leaving more complete...
– Alisson Marqui
@Alissonmarqui, I need to take the values returned (one by one), to assemble my menu, through these returns, which for now will have the options "Main Menu" and "Archives"
– user149429
Great @Alissonmarqui... I’ll take the test, thank you very much
– user149429
I tried what you said @Alissonmarqui, and it was really great, but I need to create a new list, with every data of my for... Then each item he adds will be added a new item to the new object list
– user149429
@Samuelmachado you are already with a list and are doing go on her, her
List
is the current Row object, you want to generate a List of what? I don’t understand why you need to generate a new List...– Alisson Marqui
I did the editing with XML
– novic
@Samuelmachado edited the answer exemplifying the generation of Nav-bar, according to what I understood from his Dit and the comments.
– Alisson Marqui