Home>

donet2.0 requires the help of newtonsoft.json.dll

code show as below:

using system;
using system.io;
using system.text;
using newtonsoft.json;
namespace offlineacceptcontrol.uctools
{
  public class jsontools
  {
    //Generate json string from an object information
    public static string objecttojson (object obj)
    {
      return javascriptconvert.serializeobject (obj);
    }
    //Generate object information from a json string
    public static object jsontoobject (string jsonstring, object obj)
    {
      return javascriptconvert.deserializeobject (jsonstring, obj.gettype ());
    }
  }
}

donet3.5 comes with dll processing json string

Note the references:system.runtime.serialization, system.servicemodel.web

code show as below:

using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.text;
using system.runtime.serialization;
using system.runtime.serialization.json;
namespace crjiiofflineaccept.crjiitools
{
  public class jsontools
  {
    //Generate json string from an object information
    public static string objecttojson (object obj)
    {
      datacontractjsonserializer serializer=new datacontractjsonserializer (obj.gettype ());
      memorystream stream=new memorystream ();
      serializer.writeobject (stream, obj);
      byte [] databytes=new byte [stream.length];
      stream.position=0;
      stream.read (databytes, 0, (int) stream.length);
      return encoding.utf8.getstring (databytes);
    }
    //Generate object information from a json string
    public static object jsontoobject (string jsonstring, object obj)
    {
      datacontractjsonserializer serializer=new datacontractjsonserializer (obj.gettype ());
      memorystream mstream=new memorystream (encoding.utf8.getbytes (jsonstring));
      return serializer.readobject (mstream);
    }
  }
}
c
  • Previous C # programming method to add rows to DataTable
  • Next The usage and difference of return, return true, return false in javascript