4

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 ?

Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
  • Possible duplicate of http://stackoverflow.com/questions/9110724/serializing-a-list-to-json – Avi K. Jul 01 '16 at 21:23

2 Answers2

8

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"
   },
   ...
]
Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
0

Something like the following may do the trick

JObject json = new JObject();
var finalJson = json.Serialize(dd);
Ingenioushax
  • 718
  • 5
  • 20