public JsonStructureInfo ParseStructure(string json) { var structure = new JsonStructureInfo(); var jt = JToken.Parse(json); switch (jt.Type) { case JTokenType.Object: ParseJsonObject((JObject)jt, structure, structure.Data, structure.Model); break; case JTokenType.Array: ParseJsonArray((JArray)jt, structure, structure.Data, structure.Model); break; default: var keyNode = jt.Path; var elementArray = new JsonElement { Key = keyNode, Value = jt.ToString(), Childs = new List <JsonElement>(), }; structure.MappedData.Add(keyNode, "result"); structure.Data.Childs.Add(elementArray); var epuredKey = EpureKey(keyNode); if (!structure.MappedModel.ContainsKey(epuredKey)) { if (string.IsNullOrEmpty(epuredKey)) { epuredKey = "result"; } var structureElement = new JsonStructure { Key = epuredKey, Description = "result", Childs = new List <JsonStructure>() }; structure.MappedModel.Add(epuredKey, structureElement); structure.Model.Childs.Add(structureElement); } break; } return(structure); }
public DataTable ExtractDatatable(string json, JsonStructureInfo structure) { var dt = CreateDataTable(json); // Rimuovo le colonne non configurate //TODO: // Recupero i dati parsati List <RowParsed> parsedData = ParseValues(json); // Recupero le righe più estreme e da quelle recupero il prodotto cartesiano foreach (var pd in parsedData) { var listOuput = new List <RowParsed>(); FindChildRowRecursive(pd, listOuput); LoadDrRecursive(listOuput, dt); } return(dt); }
private void ParseJsonArray(JArray ja, JsonStructureInfo structureInfo, JsonElement parent, JsonStructure parentStructure, string optionlDesc = "") { var keyNodeArray = ja.Path; var elementArray = new JsonElement { Key = keyNodeArray, Value = string.Empty, Childs = new List <JsonElement>(), IsArray = true }; structureInfo.MappedData.Add(keyNodeArray, string.Empty); parent.Childs.Add(elementArray); var epuredKeyArray = EpureKey(keyNodeArray); JsonStructure structureArray; if (!structureInfo.MappedModel.ContainsKey(epuredKeyArray)) { structureArray = new JsonStructure { Key = epuredKeyArray, Description = optionlDesc + "[]", Childs = new List <JsonStructure>() }; structureInfo.MappedModel.Add(epuredKeyArray, structureArray); parentStructure.Childs.Add(structureArray); } else { structureArray = structureInfo.MappedModel[epuredKeyArray]; } for (int i = 0; i < ja.Count; i++) { JToken jt = ja[i]; switch (jt.Type) { case JTokenType.Object: ParseJsonObject(((JObject)jt), structureInfo, elementArray, structureArray); break; case JTokenType.Array: ParseJsonArray((JArray)jt, structureInfo, elementArray, structureArray); break; case JTokenType.None: case JTokenType.Constructor: case JTokenType.Comment: case JTokenType.Null: case JTokenType.Undefined: break; case JTokenType.Property: case JTokenType.Integer: case JTokenType.Float: case JTokenType.String: case JTokenType.Boolean: case JTokenType.Date: case JTokenType.Raw: case JTokenType.Bytes: case JTokenType.Guid: case JTokenType.Uri: case JTokenType.TimeSpan: var keyNode = jt.Path; var element = new JsonElement { Key = keyNode, Value = jt.ToString(), //value Childs = new List <JsonElement>() }; structureInfo.MappedData.Add(keyNode, jt.ToString()); //value elementArray.Childs.Add(element); var epuredKey = EpureKey(keyNode); if (!structureInfo.MappedModel.ContainsKey(epuredKey)) { var jse = new JsonStructure { Key = epuredKey, Description = "value", Childs = new List <JsonStructure>() }; structureInfo.MappedModel.Add(epuredKey, jse); structureArray.Childs.Add(jse); } break; default: throw new ArgumentOutOfRangeException(); } } }
private void ParseJsonObject(JObject ja, JsonStructureInfo structureInfo, JsonElement parent, JsonStructure structureParent) { foreach (JProperty prop in ja.Properties()) { if (prop.Value.Type == JTokenType.Array) { ParseJsonArray((JArray)prop.Value, structureInfo, parent, structureParent, prop.Name); } else if (prop.Value.Type == JTokenType.Object) { var keyNode = prop.Value.Path; var element = new JsonElement { Key = keyNode, Value = string.Empty, Childs = new List <JsonElement>() }; structureInfo.MappedData.Add(keyNode, ""); parent.Childs.Add(element); var epuredKey = EpureKey(keyNode); JsonStructure jse; if (!structureInfo.MappedModel.ContainsKey(epuredKey)) { jse = new JsonStructure { Key = epuredKey, Description = prop.Name, Childs = new List <JsonStructure>() }; structureInfo.MappedModel.Add(epuredKey, jse); structureParent.Childs.Add(jse); } else { jse = structureInfo.MappedModel[epuredKey]; } ParseJsonObject((JObject)prop.Value, structureInfo, element, jse); } else { var keyNode = prop.Value.Path; var element = new JsonElement { Key = keyNode, Value = prop.Value.ToString(), Childs = new List <JsonElement>() }; structureInfo.MappedData.Add(keyNode, prop.Value.ToString()); parent.Childs.Add(element); var epuredKey = EpureKey(keyNode); if (!structureInfo.MappedModel.ContainsKey(epuredKey)) { var jse = new JsonStructure { Key = epuredKey, Description = prop.Name, Childs = new List <JsonStructure>() }; structureInfo.MappedModel.Add(epuredKey, jse); structureParent.Childs.Add(jse); } } } }