Click here to Skip to main content
12,348,901 members (67,697 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# JSON
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 3-Sep-12 2:38am
Edited 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 instance
aspnet_regiis 3-Sep-12 12:14pm
   
'proper' is not a W3C compliant standard. So you cannot deserialize the JSON data to 'proper' format...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Use fastJSON[^]'s Parse() method it will give you Dictionary<string,object> representation of the JSON which you can traverse.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

JavaScriptSerializer this is inbuilt class in .net framework

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

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
  Permalink  
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 4 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100