Skip to content

Kartessian/JSON-Sharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JSON-Sharp

Libraries and utilities in C# to interact with JSON

Extensions

This class contain three simple extensions to parse Datatables into a json object array or a javascript array.

  
  DataTable dt = new DataTable(); 
  
  //... fill your datatable here ...
  
  SqlCommand comm = new SqlCommand("select firstName, lastName, id, type from some_table", _conn);
  comm.CommandType = CommandType.StoredProcedure;
  SqlDataAdapter oDataAdapter = new SqlDataAdapter(comm);
  oDataAdapter.Fill(oDataTable);
  oDataAdapter.Dispose();
  comm.Dispose();
  
  string json = dt.ToJson();
  
  string jarray = dt.ToJavaScriptArray();
  
  string condensed = dt.ToJsonTable();
  

The expected result:

json = [
        {firstName : "john", lastName : "smith", id : 12, type: null },
        {firstName : "value", lastName : "value", id : 13, type: "book" },
        {firstName : "stuart", lastName : "lopez", id : 14, type: "movie" }
      ]
        
jarray = [
          ["john", "smith", 12, null],
          ["paul", "simons", 13, "book"],
          ["stuart", "lopez", 14, "movie"]
        ]

condensed = {columns: ["firstName", "lastName", "id", "type"],
          data: [
            ["paul", "simons", 13, "book"],
            ["paul", "simons", 13, "book"],
            ["stuart", "lopez", 14, "movie"]
          ]

The ToJsonTable method will return a json object with two root elements: "columns" that includes the name of the columns in the table in order of appearance, and "data", that is the same as the ToJavaScriptArray method.

If you have latitude and longitude columns in your DataTable you can also generate a valid JSON respone

  string geojson = dt.ToGeoJSON(latColumn, lngColumn);
  

Tools

This class requires JSON.Net from Newtonsoft. Currently this class is intended to analyze a json source and return another json object with the Interface of the Object.

Let's supose we have these two valid json strings:

json = [
        {firstName : "john", lastName : "smith", id : 12, type: null },
        {firstName : "value", lastName : "value", id : 13, type: "book" },
        {firstName : "stuart", lastName : "lopez", id : 14, type: "movie" }
      ]
        
jarray = [
          ["john", "smith", 12, null],
          ["paul", "simons", 13, "book"]
          ["stuart", "lopez", 14, "movie"]
        ]
        
string result_json = AnalyzeJson(json);

string result_jarray = AnalyzeJson(jarray);

Expected result:

result_json = {
            type: "array", 
            items: [
              { type:"object",
                items: [
                  { name: "firstName", 
                    type: "string" },
                  { name: "lastName",
                    type: "string" },
                  { name: "id",
                    type: "number" },
                  { name: "type",
                    type: "string" }
                ]
              }
            ]
          }
        
result_jarray = {
            type: "array",
            items: [
              { type: "string" },
              { type: "string" },
              { type: "number" },
              { type: "string" }
            ]
          }
        

About

Libraries and utilities in C# to interact with JSON

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors