Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert an array of objects to json

Tags:

json

c#

json.net

I have an array of objects. now I want to convert them to json.

var dd =new MyUser[10];
        for (int i = 0; i < 10; i++)
        {
            Debug.Log(i);
            dd[i] = new MyUser();
            dd[i].Status = 1;
            dd[i].TokenReg = "wsdfaf";
        }

how can I convert dd array to json ?

like image 322
S.M_Emamian Avatar asked Oct 26 '25 14:10

S.M_Emamian


1 Answers

The simplest solution might be to use JSON.NET:

string json = JsonConvert.SerializeObject(dd);

You can install it via NuGet:

 PM> Install-Package Newtonsoft.Json 

Have a look at the project page.

(You can also download it for free if you use Unity)

The output may look something like the following:

[
   {
       "Status":1,
       "TokenReg":"wsdfaf"
   },
   {
       "Status":1,
       "TokenReg":"wsdfaf"
   },
   {
       "Status":1,
       "TokenReg":"wsdfaf"
   },
   ...
]
like image 79
Jan Köhler Avatar answered Oct 29 '25 04:10

Jan Köhler