==Author==
[mailto:schoen@defectivestudios.com Matt Schoen] of [http://www.defectivestudios.com Defective Studios]
==Download==
[[Media:JSONObject.zip|Download JSONObject.zip]]
= Intro =
I came across the need to send structured data to and from a server on one of my projects, and figured it would be worth my while to use JSON. When I looked into the issue, I tried a few of the C# implementations listed on [http://json.org json.org], but found them to be too complicated to work with and expand upon. So, I've written a very simple JSONObject class, which can be generically used to encode/decode data into a simple container. This page assumes that you know what JSON is, and how it works. It's rather simple, just go to json.org for a visual description of the encoding format.
As an aside, this class is pretty central to the [[AssetCloud]] content management system, from Defective Studios.
Update: The code has been updated to version 1.4 to incorporate user-submitted patches and bug reports. This fixes issues dealing with whitespace in the format, as well as empty arrays and objects, and escaped quotes within strings.
= Usage =
Users should not have to modify the JSONObject class themselves, and must follow the very simple proceedures outlined below:
Sample data (in JSON format):
<nowiki>
{
"TestObject": {
"SomeText": "Blah",
"SomeObject": {
"SomeNumber": 42,
"SomeBool": true,
"SomeNull": null
},
"SomeEmptyObject": { },
"SomeEmptyArray": [ ],
"EmbeddedObject": "{\"field\":\"Value with \\\"escaped quotes\\\"\"}"
}
}</nowiki>
= Features =
*Decode JSON-formatted strings into a usable data structure
*Encode structured data into a JSON-formatted string
*Interoperable with Dictionary and WWWForm
*Optimized parse/stringify functions -- minimal (unavoidable) garbage creation
*Asynchronous stringify function for serializing lots of data without frame drops
*MaxDepth parsing will skip over nested data that you don't need
*Special (non-compliant) "Baked" object type can store stringified data within parsed objects
*Copy to new JSONObject
*Merge with another JSONObject (experimental)
*Random access (with [int] or [string])
*ToString() returns JSON data with optional "pretty" flag to include newlines and tabs
*Switch between double and float for numeric storage depending on level of precision needed (and to ensure that numbers are parsed/stringified correctly)
*Supports Infinity and NaN values
*JSONTemplates static class provides serialization functions for common classes like Vector3, Matrix4x4
*Object pool implementation (experimental)
*Handy JSONChecker window to test parsing on sample data
It should be pretty obvious what this parser can and cannot do. If anyone reading this is a JSON buff (is there such a thing?) please feel free to expand and modify the parser to be more compliant. Currently I am using the .NET System.Convert namespace functions for parsing the data itself. It parses strings and numbers, which was all that I needed of it, but unless the formatting is supported by System.Convert, it may not incorporate all proper JSON strings. Also, having never written a JSON parser before, I don't doubt that I could improve the efficiency or correctness of the parser. It serves my purpose, and hopefully will help you with your project! Let me know if you make any improvements :)
Also, you JSON buffs (really, who would admit to being a JSON buff...) might also notice from my feature list that this thing isn't exactly to specifications. Here is where it differs:
*"a string" is considered valid JSON. There is an optional "strict" parameter to the parser which will bomb out on such input, in case that matters to you.
*The "Baked" mode is totally made up.
*The "MaxDepth" parsing is totally made up.
*NaN and Infinity aren't officially supported by JSON ([http://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript read more] about this issue... I lol'd @ the first comment on the first answer)
*I have no idea about edge cases in my parsing strategy. I have been using this code for about 3 years now and have only had to modify the parser because other people's use cases (still valid JSON) didn't parse correctly. In my experience, anything that this code generates is parsed just fine.
== Encoding ==
Encoding is something of a hard-coded process. This is because I have no idea what your data is! It would be great if this were some sort of interface for taking an entire class and encoding it's number/string fields, but it's not. I've come up with a few clever ways of using loops and/or recursive methods to cut down of the amount of code I have to write when I use this tool, but they're pretty project-specific.
Note: This section used to be WRONG! And now it's OLD! Will update later... this will all still work, but there are now a couple of ways to skin this cat.
<syntaxhighlight lang="csharp">
//Note: your data can only be numbers and strings. This is not a solution for object serialization or anything like that.
JSONObject j = new JSONObject(JSONObject.Type.OBJECT);
//number
j.AddField("field1", 0.5);
//string
j.AddField("field2", "sampletext");
//array
JSONObject arr = new JSONObject(JSONObject.Type.ARRAY);
j.AddField("field3", arr);
arr.Add(1);
arr.Add(2);
arr.Add(3);
string encodedString = j.print();
</syntaxhighlight>
NEW! The constructor, Add, and AddField functions now support a nested delegate structure. This is useful if you need to create a nested JSONObject in a single line. For example:
<syntaxhighlight lang="csharp">
DoRequest(URL, new JSONObject(delegate(JSONObject request) {
request.AddField("sort", delegate(JSONObject sort) {
sort.AddField("_timestamp", "desc");
});
request.AddField("query", new JSONObject(delegate(JSONObject query) {
query.AddField("match_all", JSONObject.obj);
}));
request.AddField("fields", delegate(JSONObject fields) {
fields.Add("_timestamp");
});
}).ToString());
</syntaxhighlight>
== Decoding ==
Decoding is much simpler on the input end, and again, what you do with the JSONObject will vary on a per-project basis. One of the more complicated way to extract the data is with a recursive function, as drafted below. Calling the constructor with a properly formatted JSON string will return the root object (or array) containing all of its children, in one neat reference! The data is in a public ArrayList called list, with a matching key list (called keys!) if the root is an Object. If that's confusing, take a glance over the following code and the print() method in the JSONOBject class. If there is an error in the JSON formatting (or if there's an error with my code!) the debug console will read "improper JSON formatting".
<syntaxhighlight lang="csharp">
string encodedString = "{\"field1\": 0.5,\"field2\": \"sampletext\",\"field3\": [1,2,3]}";
JSONObject j = new JSONObject(encodedString);
accessData(j);
//access data (and print it)
void accessData(JSONObject obj){
switch(obj.type){
case JSONObject.Type.OBJECT:
for(int i = 0; i < obj.list.Count; i++){
string key = (string)obj.keys[i];
JSONObject j = (JSONObject)obj.list[i];
Debug.Log(key);
accessData(j);
}
break;
case JSONObject.Type.ARRAY:
foreach(JSONObject j in obj.list){
accessData(j);
}
break;
case JSONObject.Type.STRING:
Debug.Log(obj.str);
break;
case JSONObject.Type.NUMBER:
Debug.Log(obj.n);
break;
case JSONObject.Type.BOOL:
Debug.Log(obj.b);
break;
case JSONObject.Type.NULL:
Debug.Log("NULL");
break;
}
}
</syntaxhighlight>
NEW! Decoding now also supports a delegate format which will automatically check if a field exists before processing the data, providing an optional parameter for an OnFieldNotFound response. For example:
<syntaxhighlight lang="csharp">
new JSONObject(data);
list.GetField("hits", delegate(JSONObject hits) {
hits.GetField("hits", delegate(JSONObject hits2) {
没有合适的资源?快使用搜索试试~ 我知道了~
unity3D期末大作业-飞机大战游戏源码+使用文档(95分以上项目)
共2000个文件
meta:1019个
mat:186个
prefab:161个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 112 浏览量
2024-06-12
06:44:18
上传
评论 3
收藏 649.9MB ZIP 举报
温馨提示
unity3D期末大作业-飞机大战游戏源码+使用文档(95分以上项目)高分项目期末大作业开发的97分高分设计项目,可作为高分课程设计和期末大作业的参考,含有代码注释小白也可看的懂,有能力的小伙伴也可以在此基础上进行二开,项目代码完整下载即可运行。 unity3D期末大作业-飞机大战游戏源码+使用文档(95分以上项目)高分项目期末大作业开发的97分高分设计项目,可作为高分课程设计和期末大作业的参考,含有代码注释小白也可看的懂,有能力的小伙伴也可以在此基础上进行二开,项目代码完整下载即可运行。 unity3D期末大作业-飞机大战游戏源码+使用文档(95分以上项目)高分项目期末大作业开发的97分高分设计项目,可作为高分课程设计和期末大作业的参考,含有代码注释小白也可看的懂,有能力的小伙伴也可以在此基础上进行二开,项目代码完整下载即可运行。 unity3D期末大作业-飞机大战游戏源码+使用文档(95分以上项目)高分项目期末大作业开发的97分高分设计项目,可作为高分课程设计和期末大作业的参考,含有代码注释小白也可看的懂,有能力的小伙伴也可以在此基础上进行二开,项目代码完整下载即可运行。
资源推荐
资源详情
资源评论
收起资源包目录
unity3D期末大作业-飞机大战游戏源码+使用文档(95分以上项目) (2000个子文件)
RobotBoyCrouchingWalk.anim 8KB
GunFire.anim 8KB
RobotBoyIdle.anim 6KB
RobotBoyCrouch.anim 5KB
GunIdle.anim 5KB
RobotBoyWalk.anim 4KB
RobotBoyRun.anim 4KB
RobotBoyFalling.anim 1KB
RobotBoyJump11.anim 1KB
RobotBoyJump02.anim 1KB
RobotBoyJump09.anim 1KB
RobotBoyJump06.anim 1KB
RobotBoyJump01.anim 1KB
RobotBoyJump04.anim 1KB
RobotBoyJump07.anim 1KB
RobotBoyJump08.anim 1KB
RobotBoyJump10.anim 1KB
RobotBoyJump03.anim 1KB
RobotBoyJump05.anim 1KB
Session20160310PrettyIslandPrettyIsland_GaiaResources.asset 1.14MB
Session20160311RiverIsland_GaiaResources.asset 1.14MB
LightingData.asset 13KB
EffectsProfile.asset 8KB
Physics2DSettings.asset 1KB
NavMeshAreas.asset 1KB
Session20160310PrettyIslandPrettyIsland_GaiaDefaults.asset 1KB
Session20160311RiverIsland_GaiaDefaults.asset 1KB
Readme.asset 1KB
DynamicsManager.asset 1KB
PresetManager.asset 824B
Terrain_0_0-20200301-115547_5.asset 803B
Terrain_0_0-20190612-234821_3.asset 803B
Terrain_0_0-20200301-115547_7.asset 803B
Terrain_0_0-20190612-234821_0.asset 803B
Terrain_0_0-20200301-115547_4.asset 803B
Terrain_0_0-20190612-234821_7.asset 803B
Terrain_0_0-20200301-115547_3.asset 803B
Terrain_0_0-20190612-234821_2.asset 803B
Terrain_0_0-20200301-115547_6.asset 803B
Terrain_0_0-20190612-234821_1.asset 803B
Terrain_0_0-20190612-234821_4.asset 801B
Terrain_0_0-20200301-115547_1.asset 801B
Terrain_0_0-20200301-115547_2.asset 801B
Terrain_0_0-20190612-234821_6.asset 801B
Terrain_0_0-20200301-115547_0.asset 801B
Terrain_0_0-20190612-234821_5.asset 801B
AudioManager.asset 377B
VFXManager.asset 273B
TimeManager.asset 195B
NetworkManager.asset 159B
Terrain_0_0-20200301-115547.asset 133B
Terrain_0_0-20190612-234821.asset 133B
NavMesh.asset 131B
ProjectSettings.asset 130B
UnityConnectSettings.asset 129B
QualitySettings.asset 129B
InputManager.asset 129B
PackageManagerSettings.asset 129B
EditorSettings.asset 129B
GraphicsSettings.asset 129B
CarAI.asset 129B
TimelineSettings.asset 128B
AutoStreamingSettings.asset 128B
XRSettings.asset 128B
UPRSettings.asset 128B
EditorBuildSettings.asset 128B
TagManager.asset 128B
VersionControlSettings.asset 128B
CharacterThirdPersonAI.asset 127B
ClusterInputManager.asset 120B
MudRockyNormals.bmp 132B
MudRockyAlbedoSpecular.bmp 132B
libsfw.bundle 53KB
iOSOpusInteropHelper.c 567B
WaterInclude.cginc 7KB
Gun.controller 4KB
JSONObject.cs 30KB
Water.cs 16KB
WaypointCircuit.cs 13KB
PlanarReflection.cs 9KB
HealthBar.cs 8KB
ParticleSceneControls.cs 7KB
AutoMobileShaderSwitch.cs 7KB
VectorTemplates.cs 7KB
TimedObjectActivator.cs 7KB
WaypointProgressTracker.cs 6KB
HealthBarInspector.cs 6KB
HealthBarCreator.cs 6KB
ProtectCameraFromWallClip.cs 6KB
AutoCam.cs 6KB
FreeLookCam.cs 5KB
SimpleMouseRotator.cs 4KB
ReadmeEditor.cs 4KB
AbstractTargetFollower.cs 3KB
ActivateTrigger.cs 3KB
ExtensionLoader.cs 3KB
TargetFieldOfView.cs 3KB
JSONChecker.cs 3KB
DragRigidbody.cs 3KB
AfterburnerPhysicsForce.cs 3KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
王二空间
- 粉丝: 6698
- 资源: 2023
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功