Link to home
Start Free TrialLog in
Avatar of Spike UK
Spike UKFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How dio I create a JSON array in vb.net using Newtonsoft.JSON

How do I create a JSON array in vb.net using Newtonsoft.JSON (json.net) from this xml string;

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <version>2</version>
  <name>ListResult1</name>
  <model_name>ENGLISH</model_name>
  <my_list>test</my_list>
  <myitems>
    <item>
      <name>thing1</name>
      <enabled>true</enabled>
      <threshold>0.5</threshold>
      <types1/>
    </item>
    <item>
      <name>thing2</name>
      <enabled>true</enabled>
      <threshold>0</threshold>
      <types1s/>
    </item>
    <item>
      <name>thing3</name>
      <enabled>true</enabled>
      <threshold>0.4</threshold>
      <types1>
        <item>
          <otherthing1>text2</otherthing1>
          <enabled>true</enabled>
          <otherthing2>0</otherthing2>
          <otherthing3>true</otherthing3>
        </item>
      </types1>
    </item>
  </myitems>
</result>
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

You simply can convert using this code below-

string xml = @"<?xml version="1.0" encoding="UTF-8"?>
<result>
  <version>2</version>
  <name>ListResult1</name>
  <model_name>ENGLISH</model_name>
  <my_list>test</my_list>
  <myitems>
    <item>
      <name>thing1</name>
      <enabled>true</enabled>
      <threshold>0.5</threshold>
      <types1/>
    </item>
    <item>
      <name>thing2</name>
      <enabled>true</enabled>
      <threshold>0</threshold>
      <types1s/>
    </item>
    <item>
      <name>thing3</name>
      <enabled>true</enabled>
      <threshold>0.4</threshold>
      <types1>
        <item>
          <otherthing1>text2</otherthing1>
          <enabled>true</enabled>
          <otherthing2>0</otherthing2>
          <otherthing3>true</otherthing3>
        </item>
      </types1>
    </item>
  </myitems>
</result>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

string json = JsonConvert.SerializeXmlNode(doc);

Console.WriteLine(json);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Spike UK

ASKER

Very clear - thanks