How to create JSON Object and JSON String in Unity C#

  • Creation of string from Json data.
    For example, consider a Json data which contains details of a city object is saved in string data type.
using UnityEngine;

public class City : MonoBehaviour
{
public string cityName;
public int districts;
public float income;

public string SaveToString()
{
return JsonUtility.ToJson(this);
}

// Given:
// cityName = "Berlin"
// districts = 12
// income = 90000.87f
// SaveToString returns:
// {"cityName":"Berlin","districts":12,"income":90000.87}
}
  • Creation of an object from Json data.
    For example, consider a Json data which contains details of a city object is saved in object data type.
using UnityEngine;

[System.Serializable]
public class City
{
public string cityName;
public int districts;
public float income;

public static City CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<City>(jsonString);
}

// Given JSON input:
// {"cityName":"Berlin","districts":12,"income":90000.87}
// this example will return a City object with
// cityName = "Berlin", districts = 12, and income = 90000.87f.
}

--

--

--

Software Engineer - (Android, iOS and Unity) | M.Sc. Computer Science from Technische Universitaet Berlin | Mobile Advertising, attribution, monetisation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My First Ruby Gem

Database Migration Service Connectivity — A technical introspective

Your first Linux distro should be Pop!_OS

Inferno: 1 Vulnhub (Details)

Top 20 Questions For Docker Interview In 2020.

Apache Spark Executor Memory Architecture

What kind of questions will I face with in the ISTQB Foundation Level exam?

The Best Free IDEs for Python Beginners

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akarsh SEGGEMU

Akarsh SEGGEMU

Software Engineer - (Android, iOS and Unity) | M.Sc. Computer Science from Technische Universitaet Berlin | Mobile Advertising, attribution, monetisation

More from Medium

HCC Risk adjustment Coding App — Inferscience

MY JOURNEY WITH DLITHE BOOTCAMP-JAVA FULLSTACK DEVELOPMENT|Week 4(Apr11-Apr17)

HUFFMAN CODING

Day 42 Training at Ryaz: DOM in Js completed.