Click here to Skip to main content
14,970,304 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting dynamic json data form of nested multidimensional array data is dynamic so i cant define class and set the properties, now how can i get all keys and values of data in a proper format in c#.
Posted
Updated 3-Sep-12 2:39am
v2
Comments
CodeHawkz 3-Sep-12 10:29am
   
How are we supposed to know what your 'proper format' is? :S Tell us how you want to the data to end up? E.g. In a Dictionary<string, object=""> instance
bbirajdar 3-Sep-12 12:14pm
   
'proper' is not a W3C compliant standard. So you cannot deserialize the JSON data to 'proper' format...

Use fastJSON[^]'s Parse() method it will give you Dictionary<string,object> representation of the JSON which you can traverse.
   
JavaScriptSerializer this is inbuilt class in .net framework

C#
using System.Web.Script.Serialization;

public void Deserialize()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();

            string jScript = "{NAME:'SAMPLE', AGE:'00',INTEREST:'CODEPROJECT'}";
            
            EmployeeRecord emp = js.Deserialize<employeerecord>(jScript);
        }

public class EmployeeRecord
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Interest { get; set; }
    }

In case JSON is in array, use list to convert to c# objects

C#
string jScript = "[{NAME:'SAMPLE', AGE:'00',INTEREST:'CODEPROJECT'},{NAME:'SAMPLE1', AGE:'01',INTEREST:'CODEPROJECT1'}]";

List< EmployeeRecord > sc = js.Deserialize<List< EmployeeRecord >>(jScript);


for more information MSDN
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900