0

This is what coming in xml:

<data>
         <IAmArrayOfDummy>
                    <Dummy><key>key1</key><value>temp</value></Dummy>
                    <Dummy><key>key2</key><value>temp1</value></Dummy>                      
      </IAmArrayOfDummy>
</data>

And it converts into Json like this

"IAmArrayOfDummy":{"Dummy":[{"key":"key1","value":"temp"},{"key":"key2","value":"temp 
       Product"}]},

should be like this

"IAmArrayOfDummy":[{"key":"key1","value":"temp"},{"key":"key2","value":"temp 
       Product"}]

My model is like

public class Model
{
    Dummy[] IAmArrayOfDummy
}

I am using newtonsoft.JsonConvert. I added json:Array='true' but it also did not work.

Which is not correct.

I want to deserialize into array of dummy. How can I do this?

dbc
  • 104,963
  • 20
  • 228
  • 340
Rajeev Ranjan
  • 1,006
  • 8
  • 18
  • Json.NET will always add an extra level of JSON nesting for XML arrays with an outer container element. You will need to preprocess the XML or postprocess the XML manually. See [Is there anyway I can prevent the creation of an extra "object node" during converting a xml array to json using json.net?](https://stackoverflow.com/q/37662397/3744182). – dbc Jul 28 '22 at 14:38
  • In fact I think your question is a duplicate of [Is there anyway I can prevent the creation of an extra "object node" during converting a xml array to json using json.net?](https://stackoverflow.com/q/37662397/3744182) Demo of using `XNodeExtensions.FlattenCollection()` from [this answer](https://stackoverflow.com/a/37665166/3744182) to fix your XML to have one level to match your required JSON here: https://dotnetfiddle.net/h2tywi. Also, use `JsonConvert.SerializeXNode(xelement, Formatting.None, true)` to omit the outer `{"data": { }}` object when serializing. – dbc Jul 28 '22 at 14:44

1 Answers1

0

your xml is not valid it should be fixed

    var xml = @"<data>
         <IAmArrayOfDummy>
                    <Dummy><key>key1</key><value>temp</value></Dummy>
                    <Dummy><key>key2</key><value>temp1</value></Dummy>                     
      </IAmArrayOfDummy>
   </data>";

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);
    XmlNode node = xmlDoc.SelectSingleNode("data");

    var rawJson = JsonConvert.SerializeXmlNode(node);

    var model = new Model
    {
        IAmArrayOfDummy = JObject.Parse(rawJson)["data"]["IAmArrayOfDummy"]["Dummy"]
                         .ToObject<Dummy[]>()
    };

result ( as json)

{"IAmArrayOfDummy":[{"key":"key1","value":"temp"},{"key":"key2","value":"temp1"}]}
Serge
  • 40,935
  • 4
  • 18
  • 45