none
dynamic variable names JSON to c# object RRS feed

  • Question

  • hi every one i recovered form an API this JSON text 

    {
      "arc": {
        "4467541": [
          "L'arc existe dj dans le RFU#WARNING"
        ],
        "4467542": [
          "L'arc existe dj dans le RFU#WARNING"
        ],
        "4467543": [
          "L'arc existe dja dans le RFU#WARNING"
        ],
        "": [
          "Ajout de l'arc dans la topologie.#INFO",
          "Ajout de l'arc dans la topologie.#INFO",
          "Ajout de l'arc dans la topologie.#INFO",
          "Ajout de l'arc dans la topologie.#INFO"
        ],
        "4467540": [
          "L'arc existe dj dans le RFU #WARNING"
        ]
      }
    }
    as u can see the variable that comes after arc is alwase a deferent numbre some time it's empthy , dose any one have an idea how to map this JSON to C# object ?, i've tryed JsonConvert.DeserializeObject(JsonText); but i still want to map it to a specific  object 


    • Edited by hessoka Tuesday, November 29, 2016 11:46 AM
    Tuesday, November 29, 2016 11:44 AM

Answers

  • If "arc" is a permanent part, then check this example:

    [Serializable]
    class MyData
    {
        public Dictionary<string, string[]> arc;
    }
    
    . . .
    
    string json = @"
    {
        ""arc"": {
    	""4467541"": [
    		""L'arc existe dj dans le RFU#WARNING""
    	],
    	""4467542"": [
    		""L'arc existe dj dans le RFU#WARNING""
    	],
    	""4467543"": [
    		""L'arc existe dja dans le RFU#WARNING""
    	],
    	"""": [
    		""Ajout de l'arc dans la topologie.#INFO"",
    		""Ajout de l'arc dans la topologie.#INFO"",
    		""Ajout de l'arc dans la topologie.#INFO"",
    		""Ajout de l'arc dans la topologie.#INFO""
    	],
    	""4467540"": [
    		""L'arc existe dj dans le RFU #WARNING""
    	]
    	}
    }";
    
    var settings = new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true };
    var serializer = new DataContractJsonSerializer( typeof( MyData ), settings );
    
    MyData data;
    
    using( var ms = new MemoryStream( Encoding.Default.GetBytes( json ) ) )
    {
        data = (MyData)serializer.ReadObject( ms );
    }



    • Edited by Viorel_MVP Tuesday, November 29, 2016 1:35 PM
    • Marked as answer by hessoka Wednesday, November 30, 2016 10:27 AM
    Tuesday, November 29, 2016 1:34 PM

All replies

  • You have to go to the author of the Web API and ask he or she what is the data structure of the data that is being sent in Jason format so that you can create C# custom object with public properties that adhere to the data that's coming in as Jason so that you can do the conversion.
    Tuesday, November 29, 2016 12:12 PM
  • If "arc" is a permanent part, then check this example:

    [Serializable]
    class MyData
    {
        public Dictionary<string, string[]> arc;
    }
    
    . . .
    
    string json = @"
    {
        ""arc"": {
    	""4467541"": [
    		""L'arc existe dj dans le RFU#WARNING""
    	],
    	""4467542"": [
    		""L'arc existe dj dans le RFU#WARNING""
    	],
    	""4467543"": [
    		""L'arc existe dja dans le RFU#WARNING""
    	],
    	"""": [
    		""Ajout de l'arc dans la topologie.#INFO"",
    		""Ajout de l'arc dans la topologie.#INFO"",
    		""Ajout de l'arc dans la topologie.#INFO"",
    		""Ajout de l'arc dans la topologie.#INFO""
    	],
    	""4467540"": [
    		""L'arc existe dj dans le RFU #WARNING""
    	]
    	}
    }";
    
    var settings = new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true };
    var serializer = new DataContractJsonSerializer( typeof( MyData ), settings );
    
    MyData data;
    
    using( var ms = new MemoryStream( Encoding.Default.GetBytes( json ) ) )
    {
        data = (MyData)serializer.ReadObject( ms );
    }



    • Edited by Viorel_MVP Tuesday, November 29, 2016 1:35 PM
    • Marked as answer by hessoka Wednesday, November 30, 2016 10:27 AM
    Tuesday, November 29, 2016 1:34 PM