private void OnCreateObject(object sender, RoutedEventArgs e) { var property = (JsonPropertyModel)((CheckBox)sender).Tag; if (property.Parent[property.Name] == null) { property.Parent[property.Name] = JsonObjectModel.FromSchema(property.Schema.ActualPropertySchema); property.RaisePropertyChanged <JsonPropertyModel>(i => i.HasValue); } }
/// <summary>Opens a document from a given file name. </summary> /// <param name="fileName">The file name. </param> /// <param name="isReadOnly">The value indicating whether the document can be changed. </param> /// <returns>The task. </returns> public async Task OpenDocumentAsync(string fileName, bool isReadOnly) { var existingDocument = Documents.SingleOrDefault(d => d.FilePath == fileName); if (existingDocument != null) { SelectedDocument = existingDocument; } else { await RunTaskAsync(async token => { JsonDocumentModel document = null; // First try to load the schema from the default location var defaultSchemaPath = JsonDocumentModel.GetDefaultSchemaPath(fileName); if (File.Exists(defaultSchemaPath)) { document = await JsonDocumentModel.LoadAsync(fileName, defaultSchemaPath, ServiceLocator.Default.Resolve <IDispatcher>()); } // If no schema was found, check for a "_schema" property on the document if (document == null) { var schemaPropertyPath = JsonObjectModel.GetSchemaProperty(fileName); if (!String.IsNullOrWhiteSpace(schemaPropertyPath) && File.Exists(schemaPropertyPath)) { document = await JsonDocumentModel.LoadAsync(fileName, schemaPropertyPath, ServiceLocator.Default.Resolve <IDispatcher>()); } } // If no default schema or no schema property, prompt. if (document == null) { var result = await Messenger.Default.SendAsync(new OpenJsonDocumentMessage(Strings.OpenJsonSchemaDocumentDialog)); if (!result.Success) { return; } document = await JsonDocumentModel.LoadAsync(fileName, result.Result, ServiceLocator.Default.Resolve <IDispatcher>()); } document.IsReadOnly = isReadOnly; AddDocument(document); AddRecentFile(fileName); }); } }
private void OnAddArrayObject(object sender, RoutedEventArgs e) { var property = (JsonPropertyModel)((Button)sender).Tag; if (property.Value == null) { property.Value = new ObservableCollection <JsonTokenModel>(); } var list = (ObservableCollection <JsonTokenModel>)property.Value; var schema = property.Schema.Item; var obj = schema.Properties == null ? (JsonTokenModel) new JsonValueModel { Schema = schema } : JsonObjectModel.FromSchema(schema); obj.ParentList = list; list.Add(obj); }
// Use this for initialization void Start() { /*********************** 使用自定义结构类读取Json ******************************/ //获取Json文件 TextAsset jsonData = Resources.Load <TextAsset>("JsonData"); JsonObjectModel jsonObject = JsonMapper.ToObject <JsonObjectModel>(jsonData.text); foreach (var info in jsonObject.infoList) { Debug.Log(info.panelTypeString + " : " + info.path); } /*********************** 使用自定义结构类读取Json ******************************/ /*********************** 使用JsonData读取Json ******************************/ TextAsset jsonAsset = Resources.Load <TextAsset>("JsonData"); JsonData jsonData = JsonMapper.ToObject(jsonAsset.text); Debug.LogWarning(jsonData.Count); Debug.LogWarning(jsonData.IsArray); Debug.LogWarning(jsonData.IsObject); Debug.LogWarning(jsonData.Keys); Debug.LogWarning(jsonData.ToString()); Debug.LogWarning(jsonData.GetJsonType()); foreach (JsonData item in jsonData["infoList"]) { Debug.Log(item["panelTypeString"]); Debug.Log(item["path"]); } /*********************** 使用JsonData读取Json ******************************/ /*********************** 使用JsonData生成json ******************************/ JsonData jsonData = new JsonData(); jsonData["name"] = "马三小伙儿"; jsonData["age"] = 22; jsonData["local"] = "Beijing"; string jsonString = jsonData.ToJson(); Debug.Log("Object to json: " + jsonString); //对象中嵌套对象 JsonData data2 = new JsonData(); data2["name"] = "peiandsky"; data2["info"] = new JsonData(); data2["info"]["sex"] = "male"; data2["info"]["age"] = 28; string json2 = data2.ToJson(); Debug.Log("Object to json: " + json2); /*********************** 使用JsonData读取Json ******************************/ /*********************** 使用JsonReader ******************************/ TextAsset jsonData = Resources.Load <TextAsset>("JsonData"); JsonReader reader = new JsonReader(jsonData.text); while (reader.Read()) { string type = reader.Value != null?reader.Value.GetType().ToString() : "None"; Debug.Log(reader.Token + " " + reader.Value + " " + type); } reader.Close(); /*********************** 使用JsonReader ******************************/ /*********************** 使用JsonWriter ******************************/ //通过JsonWriter类创建一个Json对象 StringBuilder sb = new StringBuilder(); JsonWriter writer = new JsonWriter(sb); writer.WriteArrayStart(); writer.Write(1); writer.Write(2); writer.Write(3); writer.WriteObjectStart(); writer.WritePropertyName("color"); writer.Write("blue"); writer.WriteObjectEnd(); writer.WriteArrayEnd(); Debug.Log(sb.ToString()); /*********************** 使用JsonWriter ******************************/ }
public void DeserializeObject() { this.jsonObjectModel = JsonManager.DeserializeFromJson <JsonObjectModel>(jsonObjectJSON.text); textComponent.text = "JsonObject deserialized into jsonObjectModel variable inside ExampleController_JsonManager (You can see it on inspector!)"; }