C# 多個子JSON整合成一個大JSON變成字串儲存 & 將字串還原成物件

C# 多個子JSON整合成一個大JSON變成字串儲存 & 將字串還原成物件

C# 多個子JSON整合成一個大JSON變成字串儲存 & 將字串還原成物件


json to c# class converter tool:

    01.http://json2csharp.com/ 

    02.https://jsonutils.com/


線上編譯器: https://dotnetfiddle.net/


Code:

using System;
using System.IO;
using System.Text.Json;

public class Root
{
    public object easycard { get; set; }
    public object nccc { get; set; }
}

public class nccc
{
	public int a { get; set; }
	public int b { get; set; }
}

public class Program
{
	public static void Main()
	{
		nccc nccc1=new nccc();
		nccc1.a=10;
		nccc1.b=20;

		Root Root01=new Root();
		Root01.nccc=nccc1;
		Root01.easycard="{}";

		//---
		//obj -> string
		String StrResult = JsonSerializer.Serialize(Root01);
		Console.WriteLine(StrResult);
		//---
		
		//---
		//string -> obj
		Root Result = JsonSerializer.Deserialize<Root>(StrResult);
		nccc nccc2 = JsonSerializer.Deserialize<nccc>(Result.nccc.ToString());
		Console.WriteLine("nccc2.a="+nccc2.a);
		Console.WriteLine("nccc2.b="+nccc2.b);
		//---
		
	}
}

One thought on “C# 多個子JSON整合成一個大JSON變成字串儲存 & 將字串還原成物件


  1. using System;
    using System.IO;
    using System.Text.Json;

    public class Root
    {
    public object easycard { get; set; }
    public object nccc { get; set; }
    }

    public class nccc
    {
    public int a { get; set; }
    public int b { get; set; }
    }

    public class Program
    {
    public static void Main()
    {
    nccc nccc1=new nccc();
    nccc1.a=10;
    nccc1.b=20;

    Root Root01=new Root();
    Root01.nccc=JsonSerializer.Serialize(nccc1);
    Root01.easycard="{}";

    //---
    //obj -> string
    String StrResult = JsonSerializer.Serialize(Root01);
    Console.WriteLine(StrResult);
    //---

    //---
    //string -> obj
    Root Result = JsonSerializer.Deserialize(StrResult);
    nccc nccc2 = JsonSerializer.Deserialize(Result.nccc.ToString());
    Console.WriteLine("nccc2.a="+nccc2.a);
    Console.WriteLine("nccc2.b="+nccc2.b);
    //---

    }
    }

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *