-
Notifications
You must be signed in to change notification settings - Fork 1
/
Example.cs
47 lines (35 loc) · 1.93 KB
/
Example.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
JSON string:
{
"version": "1.0",
"data": {
"sampleArray": [
"string value",
5,
{
"name": "sub object"
}
]
}
}
Parsing examples:
var N = JSON.Parse(the_JSON_string);
var versionString = N["version"].Value; // versionString will be a string containing "1.0"
var versionNumber = N["version"].AsFloat; // versionNumber will be a float containing 1.0
var name = N["data"]["sampleArray"][2]["name"];// name will be a string containing "sub object"
//C#
string val = N["data"]["sampleArray"][0]; // val contains "string value"
//UnityScript
var val : String = N["data"]["sampleArray"][0];// val contains "string value"
var i = N["data"]["sampleArray"][1].AsInt; // i will be an integer containing 5
N["data"]["sampleArray"][1].AsInt = i+6; // the second value in sampleArray will contain "11"
N["additional"]["second"]["name"] = "FooBar"; // this will create a new object named "additional" in this object create another
//object "second" in this object add a string variable "name"
var mCount = N["countries"]["germany"]["moronCount"].AsInt; // this will return 0 and create all the required objects and
// initialize "moronCount" with 0.
if (N["wrong"] != null) // this won't execute the if-statement since "wrong" doesn't exist
{}
if (N["wrong"].AsInt == 0) // this will execute the if-statement and in addition add the "wrong" value.
{}
N["data"]["sampleArray"][-1] = "Test"; // this will add another string to the end of the array
N["data"]["sampleArray"][-1]["name"] = "FooBar"; // will add another object to the end of the array containing string named "name"
N["data"] = "erased"; // this will replace the object stored in data with the string "erased"