# Author
Matt Schoen <schoen@defectivestudios.com> of [Defective Studios](http://www.defectivestudios.com)
# 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, 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.
# Usage
Users should not have to modify the JSONObject class themselves, and must follow the very simple procedures outlined below:
Sample data (in JSON format):
```JSON
{
"TestObject": {
"SomeText": "Blah",
"SomeObject": {
"SomeNumber": 42,
"SomeBool": true,
"SomeNull": null
},
"SomeEmptyObject": { },
"SomeEmptyArray": [ ],
"EmbeddedObject": "{\"field\":\"Value with \\\"escaped quotes\\\"\"}"
}
}
```
The test classes provide the best examples for how the API is intended to be used.
## 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` were introduced in a later version of the standard, and some linters will report them as errors
## 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.
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:
```C#
void DoRequest(string url, string jsonString) {
// Web Request logic
}
void Test(string url) {
DoRequest(url, new JSONObject(request => {
request.AddField("sort", sort => sort.AddField("_timestamp", "desc"));
request.AddField("query", new JSONObject(query => query.AddField("match_all", JSONObject.emptyObject)));
request.AddField("fields", fields => fields.Add("_timestamp"));
}).ToString());
}
```
## 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".
```C#
void Test() {
var encodedString = "{\"field1\": 0.5,\"field2\": \"sampletext\",\"field3\": [1,2,3]}";
var jsonObject = new JSONObject(encodedString);
AccessData(jsonObject);
}
void AccessData(JSONObject jsonObject) {
switch (jsonObject.type) {
case JSONObject.Type.Object:
for (var i = 0; i < jsonObject.list.Count; i++) {
var key = jsonObject.keys[i];
var value = jsonObject.list[i];
Debug.Log(key);
AccessData(value);
}
break;
case JSONObject.Type.Array:
foreach (JSONObject element in jsonObject.list) {
AccessData(element);
}
break;
case JSONObject.Type.String:
Debug.Log(jsonObject.stringValue);
break;
case JSONObject.Type.Number:
Debug.Log(jsonObject.floatValue);
break;
case JSONObject.Type.Bool:
Debug.Log(jsonObject.boolValue);
break;
case JSONObject.Type.Null:
Debug.Log("Null");
break;
case JSONObject.Type.Baked:
Debug.Log(jsonObject.stringValue);
break;
}
}
```
Decoding 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:
```C#
void Test(string jsonString) {
var list = new JSONObject(jsonString);
list.GetField("users", users => {
foreach (var user in users.list) {
var thisUser = user;
users.GetField("sessions", sessions => {
foreach (JSONObject gameSession in sessions.list) {
Debug.Log(gameSession);
}
}, name => Debug.LogWarning(string.Format("No sessions for user {0}", thisUser["name"].stringValue)));
}
});
}
```
## `(O(n))` Random access
I've added a string and int [] index to the class, so you can now retrieve data as such (from above):
```C#
void Test() {
var jsonObject = new JSONObject("{\"field\":[0,1,2]");
var array = jsonObject["field"];
Debug.Log(array[2].intValue); //Should output "2"
}
```
## Change Log
### v2.1.3
* Fix exception where input is just an empty array or object
* Pool and null out list and keys field when clearing JSONObject for consistency with a fresh object;
This was causing the MaxDepthWithExcessLevels test to fail randomly when checking results because some pooled objects had list but not keys
* Update JSONChecker with information about pools and a note about not validating standard JSON
### v2.1.2
* Fix issue parsing json strings with whitespace after colon characters
### v2.1.1
* Fix issue parsing nested arrays with multiple elements
* Refactor MaxDepth tests
* Fi
没有合适的资源?快使用搜索试试~ 我知道了~
unity解析json对象
共21个文件
meta:11个
cs:8个
md:2个
需积分: 5 5 下载量 78 浏览量
2023-03-02
08:56:24
上传
评论
收藏 33KB ZIP 举报
温馨提示
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, 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.
资源推荐
资源详情
资源评论
收起资源包目录
JSONObject-master.zip (21个子文件)
JSONObject-master
LICENSE.md 1KB
JSONObject.cs.meta 60B
VectorTemplates.cs.meta 60B
readme.md 9KB
VectorTemplates.cs 10KB
LICENSE.md.meta 158B
readme.md.meta 60B
JSONObject.cs 49KB
Editor.meta 60B
Editor
JSONObjectTests.cs 40KB
JSONChecker.cs.meta 60B
JSONObjectVectorTemplateTests.cs.meta 243B
JSONObjectVectorTemplateTests.cs 5KB
JSONObjectEnumeratorTests.cs 2KB
JSONObjectTestStrings.cs 2KB
JSONChecker.cs 4KB
JSONObjectTestStrings.cs.meta 243B
JSONObjectTests.cs.meta 243B
JSONObjectAsyncTests.cs 11KB
JSONObjectAsyncTests.cs.meta 243B
JSONObjectEnumeratorTests.cs.meta 243B
共 21 条
- 1
资源评论
feifan20
- 粉丝: 2
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功