SoFunction
Updated on 2025-03-06

Example of C# implementation of mutual conversion function between String type and json

This article describes the mutual conversion function between String type and json in C#. Share it for your reference, as follows:

////Donet2.0 requires adding a reference// Generate a Json string from an object informationpublic static string ObjectToJson(object obj)
{
   return (obj);
}
// Generate object information from a Json stringpublic static object JsonToObject(string jsonString,object obj)
{
   return (jsonString, ());
}
//Donet3.5 comes with DLL processing json string//Note the quote:

Code

using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace 
{
  public class JsonTools
  {
    // Generate a Json string from an object information    public static string ObjectToJson(object obj)
    {
      DataContractJsonSerializer serializer = new DataContractJsonSerializer(());
      MemoryStream stream = new MemoryStream();
      (stream, obj);
      byte[] dataBytes = new byte[];
       = 0;
      (dataBytes, 0, (int));
      return Encoding.(dataBytes);
    }
    // Generate object information from a Json string    public static object JsonToObject(string jsonString, object obj)
    {
      DataContractJsonSerializer serializer = new DataContractJsonSerializer(());
      MemoryStream mStream = new MemoryStream(Encoding.(jsonString));
      return (mStream);
    }
  }
}

PS: Regarding json operation, here are some more practical json online tools for your reference:

OnlineJSONCode verification, inspection, beautification and formatting tools:
http://tools./code/json

JSONOnline formatting tools:
http://tools./code/jsonformat

Online XML/JSONConvert each other tools:
http://tools./code/xmljson

jsonCode online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat

OnlinejsonCompression/escaping tools:
http://tools./code/json_yasuo_trans

For more information about C#, you can also view the special topic of this site: "Summary of C# string operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《Summary of thread usage techniques for C# programming》、《Summary of XML file operation skills in C#》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.