-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonTest.cs
More file actions
136 lines (117 loc) · 3.71 KB
/
Copy pathJsonTest.cs
File metadata and controls
136 lines (117 loc) · 3.71 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
*┌──────────────────────────────────┐
*│ Copyright(C) 2016 by Antiphon.All rights reserved. │
*│ Author:by Locke Xie 2017-01-04 │
*└──────────────────────────────────┘
*
* 功 能:
* 类 名: JsonTest.cs
*
* 修改历史:
*
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using UnityEngine;
using UnityEngine.UI;
public class JsonTest: MonoBehaviour
{
public string url;
public string Data;
public List<Student> studens = new List<Student>();
public Dictionary<string,Student> studenlib = new Dictionary<string, Student>();
public Student stud1;
public Student stud2;
public List<Student> jsonTo;
public Dictionary<string,Student> jsonTolib;
string json;
string jsonlib;
string jsonMsg;
public MahjongMessage mahjongMsg = new MahjongMessage();
public MahjongMessage mahjongMessage;
HTTPUnity http = new HTTPUnity();
void Start()
{
stud1 = new Student(2, "haha", 22, "boy");
stud2 = new Student(5, "BiuBiuBiu", 55, "girl");
studens.Add(stud1);
studens.Add(stud2);
studenlib.Add(stud1.Name, stud1);
studenlib.Add(stud2.Name, stud2);
mahjongMsg.data = new Data();
mahjongMsg.extra = new Extra();
mahjongMsg.message = new List<ServerException>();
mahjongMsg.status = 1;
mahjongMsg.message.Add(new ServerException("1001", "失败"));
mahjongMsg.message.Add(new ServerException("1002", "失败"));
}
void OnGUI()
{
if(GUILayout.Button("连接Url"))
{
StartCoroutine(http.GET(url));
}
if(GUILayout.Button("Send"))
{
StartCoroutine(http.PostBit(url, Data));
}
if(GUILayout.Button("msgTojson"))
{
jsonMsg = JsonUtility.ToJson(mahjongMsg);
StartCoroutine(http.PostBit(url, jsonMsg));
Debug.Log(jsonMsg);
}
if(GUILayout.Button("jsonTomsg"))
{
mahjongMessage = JsonUtility.FromJson<MahjongMessage>(jsonMsg);
Debug.Log(mahjongMessage.ToString());
}
if(GUILayout.Button("list<t>->json"))
{
json = JsonUtility.ToJson(new Serialization<Student>(studens));
http.PostBit(url, json);
Debug.Log(json);
}
if(GUILayout.Button("Dictionary<k,v>->json"))
{
jsonlib = JsonUtility.ToJson(new Serialization<string, Student>(studenlib));
http.PostBit(url, jsonlib);
Debug.Log(jsonlib);
}
if(GUILayout.Button("json->list<t>"))
{
jsonTo = JsonUtility.FromJson<Serialization<Student>>(json).ToList();
if(jsonTo != null)
foreach(Student student in jsonTo)
Debug.Log(student.ToString());
}
if(GUILayout.Button("json->Dictionary<k,v>"))
{
jsonTolib = JsonUtility.FromJson<Serialization<string, Student>>(jsonlib).ToDictionary();
if(jsonTolib != null)
Debug.Log(jsonTolib.Count);
}
}
}
[Serializable]
public struct Student
{
public int ID;
public string Name;
public int Age;
public string Sex;
public Student(int id,string name,int age,string sex)
{
this.ID = id;
this.Name = name;
this.Age = age;
this.Sex = sex;
}
public override string ToString()
{
return "ID:"+ID+": Name:" + Name+": Age:"+ Age + ": Sex:" + Sex;
}
}