question

Robin-8787 avatar image
0 Votes"
Robin-8787 asked Robin-8787 commented

XML to JSON Conversion Issue

Hi,

I am trying to pull some data from the web using HttpWebRequest. I don't know how to access some variables as they are starting with a @ sign and returning 0 or null values.

This is the code I am using to pull the data.

             var httpRequest = (HttpWebRequest)WebRequest.Create(faUrl);
             httpRequest.Headers["Authorization"] = ConfigurationData.cfgToken;

             try
             {
                 var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                 using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                 {
                     result = streamReader.ReadToEnd();
                     streamReader.Close();
                 }
             }

This is what I am getting in the results variable in debug using the XML Visualizer.

134563-messageheaders.jpg


This is the code I am using to deserialize.

             XmlDocument xmlDoc = new XmlDocument();
             xmlDoc.LoadXml(result);
             var fromXml = JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.Indented);
             var jsonText = JsonConvert.DeserializeObject<RootObjects.Rootobject>(fromXml);


This is what I see in fromXML variable using the Text Visualizer.

134641-textvisualizer.jpg


Now if you see the values for _count, _startindex and _totalcount they are 0 and _xmlns is null. But if you see the previous images, they have values.

134500-jsontext.jpg


How can I get the values into my variables? I can successfully get the reference and status variables as they don't start with @.

This is the RootObject class.

 class RootObjects
 {

     public class Rootobject
     {
         public Messageheaders messageheaders { get; set; }
     }

     public class Messageheaders
     {
         public Messageheader[] messageheader { get; set; }
         public string _xmlns { get; set; }
         public int _startindex { get; set; }
         public int _count { get; set; }
         public int _totalcount { get; set; }
     }

Appreciate any help. (I am not an expert C# programmer).

Thanks,

Robin


dotnet-csharp
messageheaders.jpg (40.4 KiB)
textvisualizer.jpg (43.7 KiB)
jsontext.jpg (38.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered Robin-8787 commented

When a property starts with @ you need to alias it.

For Json.net

 [JsonProperty("@someVar")]
 public int AliasNameGoesHere { get; set; }

For System.Text.Json

 [JsonPropertyName("@someVar")]
 public int AliasNameGoesHere { get; set; }

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Karen,

That didn't work. This is the changes I did.

using System;
using System.Text.Json.Serialization;

namespace GetInput
{
class RootObjects
{

     public class Rootobject
     {
         public Messageheaders messageheaders { get; set; }
     }

     public class Messageheaders
     {
         public Messageheader[] messageheader { get; set; }

         [JsonPropertyName("@xmlns")]
         public string _xmlns { get; set; }

         [JsonPropertyName("@startindex")]
         public int _startindex { get; set; }

         [JsonPropertyName("@count")]
         public int _count { get; set; }

         [JsonPropertyName("@totalcount")]
         public int _totalcount { get; set; }

I had to use System.Text.Json.Serialization, I hope that is correct.

And then the following code:


             RootObjects.Rootobject outPutMetrics = jsonText;

             string XMLns = outPutMetrics.messageheaders._xmlns;
             int Count = outPutMetrics.messageheaders._count;
             int StartIndex = outPutMetrics.messageheaders._startindex;
             int TotalCount = outPutMetrics.messageheaders._totalcount;

Not sure what I am doing wrong but I am still getting null and zeros.




0 Votes 0 ·

Are you still deserialising jsonText like this?:

var jsonText = JsonConvert.DeserializeObject<RootObjects.Rootobject>(fromXml);


If you are then this is Json.NET, so you'd need to annotate your properties with JsonProperty instead of JsonPropertyName like Karen mentioned.

Alternatively you could deserialise your jsonText like this and keep your annotations as-is:

var obj = JsonSerializer.Deserialize<RootObjects.Rootobject>(fromXml);
1 Vote 1 ·

I didn't know the where to use Json.Net and where to use System.Text.Json. So it was my mistake. Changed to JsonProperty and it is working fine.

Thanks

0 Votes 0 ·

As @Paul-5034 recommended you should use JsonSerializer.Deserialize<List<T>>(jsonString); then set a breakpoint to inspect values in the properties in question.

I use a language extension method, sample usage.

If this fails, try using Json.Net as this is a mature library. I've seen System.Text.Json have issues with dates (yea not your concern) and had to modify Microsoft converters.


1 Vote 1 ·
Robin-8787 avatar image Robin-8787 karenpayneoregon ·

Thanks Karen. I should have used Json.Net. It is working fine. now.

Thanks again. Much appreciated your help.

Robin

0 Votes 0 ·