Skip to main content
deleted 14 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

JsonUtility.ToJson only works with objects of a class which inherits from MonoBehaviour or ScriptableObject or which has the [Serializable] attribute. So when you want to use it to create a Json message for some external API, then you should build classes with the [Serializable] attribute which mimic the JSON objects that API expects.

When that API exects a format like this:

 {"profiles": [
     {"email":"[email protected]"}, 
     {"email":"[email protected]"} ]
 }`

then you can create two classes like this:

[Serializable]
private class ApiDataProfile {
     public string email;
}
[Serializable]
private class ApiData {
     public ApiDataProfile[] profiles;
}

These classes can actually be declared private within the class which uses itthem. So you don't necessarily need to create a new sourcecode filefiles for itthem.

You can then create your JSON string by using object and collection initializers:

string data = JsonUtility.ToJson(
      new ApiData() { 
         profiles = new ApiDataProfile[] {
             new ApiDataProfile() { email = "[email protected]" },
             new ApiDataProfile() { email = "[email protected]" }
         }
      }
); 

In some cases the API might expect keys which are not valid C# identifier names. They might start with a number or are reserved C# keywords and thus can not be used as variable names. In that case you can help yourself by using the @ character in front of them:

[Serializable]
private class EmailApiData {
     public string _email;
     public string @for;
     public int @1stLoginTimestamp;
}

public void SaveData() {
    string data = JsonUtility.ToJson(new EmailApiData() { 
         _email = email.text 
         @for = someOtherString
         @1stLoginTimestamp = DateTime.Now
    }); 
    //...
}

JsonUtility.ToJson only works with objects of a class which inherits from MonoBehaviour or ScriptableObject or which has the [Serializable] attribute. So when you want to use it to create a Json message for some external API, then you should build classes with the [Serializable] attribute which mimic the JSON objects that API expects.

When that API exects a format like this:

 {"profiles": [
     {"email":"[email protected]"}, 
     {"email":"[email protected]"} ]
 }`

then you can create two classes like this:

[Serializable]
private class ApiDataProfile {
     public string email;
}
[Serializable]
private class ApiData {
     public ApiDataProfile[] profiles;
}

These classes can actually be declared private within the class which uses it. So you don't necessarily need to create a new sourcecode file for it.

You can then create your JSON string by using object and collection initializers:

string data = JsonUtility.ToJson(
      new ApiData() { 
         profiles = new ApiDataProfile[] {
             new ApiDataProfile() { email = "[email protected]" },
             new ApiDataProfile() { email = "[email protected]" }
         }
      }
); 

In some cases the API might expect keys which are not valid C# identifier names. They might start with a number or are reserved C# keywords and thus can not be used as variable names. In that case you can help yourself by using the @ character in front of them:

[Serializable]
private class EmailApiData {
     public string _email;
     public string @for;
     public int @1stLoginTimestamp;
}

public void SaveData() {
    string data = JsonUtility.ToJson(new EmailApiData() { 
         _email = email.text 
         @for = someOtherString
         @1stLoginTimestamp = DateTime.Now
    }); 
    //...
}

JsonUtility.ToJson only works with objects of a class which inherits from MonoBehaviour or ScriptableObject or which has the [Serializable] attribute. So when you want to use it to create a Json message for some external API, then you should build classes with the [Serializable] attribute which mimic the JSON objects that API expects.

When that API exects a format like this:

 {"profiles": [
     {"email":"[email protected]"}, 
     {"email":"[email protected]"} ]
 }`

then you can create two classes like this:

[Serializable]
private class ApiDataProfile {
     public string email;
}
[Serializable]
private class ApiData {
     public ApiDataProfile[] profiles;
}

These classes can be declared within the class which uses them. So you don't necessarily need to create new sourcecode files for them.

You can then create your JSON string by using object and collection initializers:

string data = JsonUtility.ToJson(
      new ApiData { 
         profiles = new ApiDataProfile[] {
             new ApiDataProfile { email = "[email protected]" },
             new ApiDataProfile { email = "[email protected]" }
         }
      }
); 

In some cases the API might expect keys which are not valid C# identifier names. They might start with a number or are reserved C# keywords and thus can not be used as variable names. In that case you can help yourself by using the @ character in front of them:

[Serializable]
private class EmailApiData {
     public string _email;
     public string @for;
     public int @1stLoginTimestamp;
}

public void SaveData() {
    string data = JsonUtility.ToJson(new EmailApiData() { 
         _email = email.text 
         @for = someOtherString
         @1stLoginTimestamp = DateTime.Now
    }); 
    //...
}
added 623 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

JsonUtility.ToJson only works with objects of a class which inherits from MonoBehaviour or ScriptableObject or which has the [Serializable] attribute. So when you want to use it to create a Json message for some external API, then you should build a classclasses with the [Serializable] attribute which mimicsmimic the JSON objectobjects that API expects.

When that API exects just { "_email":"[email protected]" },a format like this:

 {"profiles": [
     {"email":"[email protected]"}, 
     {"email":"[email protected]"} ]
 }`

then you can create a classtwo classes like this:

[Serializable]
private class EmailApiDataApiDataProfile {
     public string _email;email;
}
[Serializable]
private class ApiData {
     public ApiDataProfile[] profiles;
}

This classThese classes can actually be declared private within the class which uses it. So you don't necessarily need to create a new sourcecode file for it.

You can then create your JSON string like thisby using object and collection initializers:

string data = JsonUtility.ToJson(
      new EmailApiDataApiData() { _email
         profiles = new ApiDataProfile[] {
             new ApiDataProfile() { email = "email1@example.textcom" },
             new ApiDataProfile() { email = "[email protected]" }
         }
      }
); 

In some cases the API might expect keys which are not valid C# identifier names. They might start with a number or are reserved C# keywords and thus can not be used as variable names. In that case you can help yourself by using the @ character in front of them:

[Serializable]
private class EmailApiData {
     public string _email;
     public string @for;
     public int @1stLoginTimestamp;
}

public void SaveData() {
    string data = JsonUtility.ToJson(new EmailApiData() { 
         _email = email.text 
         @for = someOtherString
         @1stLoginTimestamp = DateTime.Now
    }); 
    //...
}

JsonUtility.ToJson only works with objects of a class which inherits from MonoBehaviour or ScriptableObject or which has the [Serializable] attribute. So when you want to use it to create a Json message for some external API, then you should build a class which mimics the JSON object that API expects.

When that API exects just { "_email":"[email protected]" }, then you can create a class like this:

[Serializable]
private class EmailApiData {
     public string _email;
}

This class can actually be declared private within the class which uses it. So you don't necessarily need to create a new sourcecode file for it.

You can then create your JSON string like this:

string data = JsonUtility.ToJson(new EmailApiData() { _email = email.text }); 

In some cases the API might expect keys which are not valid C# identifier names. They might start with a number or are reserved C# keywords and thus can not be used as variable names. In that case you can help yourself by using the @ character in front of them:

[Serializable]
private class EmailApiData {
     public string _email;
     public string @for;
     public int @1stLoginTimestamp;
}

public void SaveData() {
    string data = JsonUtility.ToJson(new EmailApiData() { 
         _email = email.text 
         @for = someOtherString
         @1stLoginTimestamp = DateTime.Now
    }); 
    //...
}

JsonUtility.ToJson only works with objects of a class which inherits from MonoBehaviour or ScriptableObject or which has the [Serializable] attribute. So when you want to use it to create a Json message for some external API, then you should build classes with the [Serializable] attribute which mimic the JSON objects that API expects.

When that API exects a format like this:

 {"profiles": [
     {"email":"[email protected]"}, 
     {"email":"[email protected]"} ]
 }`

then you can create two classes like this:

[Serializable]
private class ApiDataProfile {
     public string email;
}
[Serializable]
private class ApiData {
     public ApiDataProfile[] profiles;
}

These classes can actually be declared private within the class which uses it. So you don't necessarily need to create a new sourcecode file for it.

You can then create your JSON string by using object and collection initializers:

string data = JsonUtility.ToJson(
      new ApiData() { 
         profiles = new ApiDataProfile[] {
             new ApiDataProfile() { email = "email1@example.com" },
             new ApiDataProfile() { email = "[email protected]" }
         }
      }
); 

In some cases the API might expect keys which are not valid C# identifier names. They might start with a number or are reserved C# keywords and thus can not be used as variable names. In that case you can help yourself by using the @ character in front of them:

[Serializable]
private class EmailApiData {
     public string _email;
     public string @for;
     public int @1stLoginTimestamp;
}

public void SaveData() {
    string data = JsonUtility.ToJson(new EmailApiData() { 
         _email = email.text 
         @for = someOtherString
         @1stLoginTimestamp = DateTime.Now
    }); 
    //...
}
added 154 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

JsonUtility.ToJson only works with objects of a class which inherits from MonoBehaviour or ScriptableObject or which has the [Serializable] attribute. So when you want to use it to create a Json message for some external API, then you should build a class which mimics the JSON object that API expects.

When that API exects just { "_email":"[email protected]" }, then you can create a class like this:

[Serializable]
private class EmailApiData {
     public string _email;
}

This class can actually be declared private within the class which uses it. So you don't necessarily need to create a new sourcecode file for it.

You can then create your JSON string like this:

string data = JsonUtility.ToJson(new EmailApiData() { _email = email.text }); 

In some cases the API might expect keys which are not valid C# identifier names. They might start with a number or are reserved C# keywords and thus can not be used as variable names. In that case you can help yourself by using the @ character in front of them:

[Serializable]
private class EmailApiData {
     public string _email;
     public string @for;
     public int @1stLoginTimestamp;
} 

public void SaveData() {
    string data = JsonUtility.ToJson(new EmailApiData() { 
         _email = email.text 
         @for = someOtherString
         @1stLoginTimestamp = DateTime.Now
    }); 
    //...
}

JsonUtility.ToJson only works with objects of a class which inherits from MonoBehaviour or ScriptableObject or which has the [Serializable] attribute. So when you want to use it to create a Json message for some external API, then you should build a class which mimics the JSON object that API expects.

When that API exects just { "_email":"[email protected]" }, then you can create a class like this:

[Serializable]
private class EmailApiData {
     public string _email;
}

This class can actually be declared private within the class which uses it. So you don't necessarily need to create a new sourcecode file for it.

You can then create your JSON string like this:

string data = JsonUtility.ToJson(new EmailApiData() { _email = email.text }); 

In some cases the API might expect keys which are reserved C# keywords and thus can not be used as variable names. In that case you can help yourself by using the @ character in front of them:

[Serializable]
private class EmailApiData {
     public string _email;
     public string @for;
}

string data = JsonUtility.ToJson(new EmailApiData() { 
     _email = email.text 
     @for = someOtherString
}); 

JsonUtility.ToJson only works with objects of a class which inherits from MonoBehaviour or ScriptableObject or which has the [Serializable] attribute. So when you want to use it to create a Json message for some external API, then you should build a class which mimics the JSON object that API expects.

When that API exects just { "_email":"[email protected]" }, then you can create a class like this:

[Serializable]
private class EmailApiData {
     public string _email;
}

This class can actually be declared private within the class which uses it. So you don't necessarily need to create a new sourcecode file for it.

You can then create your JSON string like this:

string data = JsonUtility.ToJson(new EmailApiData() { _email = email.text }); 

In some cases the API might expect keys which are not valid C# identifier names. They might start with a number or are reserved C# keywords and thus can not be used as variable names. In that case you can help yourself by using the @ character in front of them:

[Serializable]
private class EmailApiData {
     public string _email;
     public string @for;
     public int @1stLoginTimestamp;
} 

public void SaveData() {
    string data = JsonUtility.ToJson(new EmailApiData() { 
         _email = email.text 
         @for = someOtherString
         @1stLoginTimestamp = DateTime.Now
    }); 
    //...
}
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345
Loading