public static DataTable json_convert(string json) { JObject json_object = JObject.Parse(json); DataTable dt = new DataTable(); Console.WriteLine($"cek masuk2"); JArray json_array = JArray.Parse(json_object["data"].ToString()); Console.WriteLine(json_array); json_array.Descendants().OfType <JProperty>() .Where(p => p.Name == "service") .ToList() .ForEach(att => att.Remove()); json_array.Descendants().OfType <JProperty>() .Where(p => p.Name == "sparepart") .ToList() .ForEach(att => att.Remove()); json_array.Descendants().OfType <JProperty>() .Where(p => p.Name == "employee") .ToList() .ForEach(att => att.Remove()); Console.WriteLine(json_array); dt = JsonConvert.DeserializeObject <DataTable>(json_array.ToString()); return(dt); }
/// <summary> /// Sanitizes a JArray. /// </summary> /// <param name="obj">JArray.</param> /// <param name="removeDocumentMetaData">Indicates whether 'id' and 'lastUpdate' should be removed.</param> public static void Sanitize(this JArray obj, bool removeDocumentMetaData = false) { List <JToken> metaData = new List <JToken>(); foreach (JToken token in obj.Descendants()) { try { JProperty propertyToken = (JProperty)token; if (propertyToken.Name.StartsWith("_")) { metaData.Add(token); } if (removeDocumentMetaData) { if (propertyToken.Name.Equals("id") || propertyToken.Name.Equals("lastUpdate")) { metaData.Add(token); } } } // Some Properties may not get Serialized, just skip them catch { } } metaData.ForEach(n => n.Remove()); }
private JToken FindNextMakroToken(JArray runtimeTemplate) { return(runtimeTemplate .Descendants() .FirstOrDefault(d => d is JValue v && v.Value is string s && s.Contains("{$") && s.Substring(s.IndexOf("{$")).Contains("}"))); }
public EventModel(JObject json) { JArray array = json.Descendants().OfType <JProperty>().First(x => x.Name == "param").Value.ToObject <JArray>(); if (array != null) { JProperty eventNrProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "EventNr"); JProperty exceptionTypeProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "ExceptionType"); JProperty categoryProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "Category"); JProperty eventTimestampProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "EventTimestamp"); JProperty hResultProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "HResult"); JProperty messageProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "Message"); JProperty stacktraceProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "Stacktrace"); this._exceptionType = exceptionTypeProperty.Value.ToString(); this._category = categoryProperty.Value.ToString(); this._message = messageProperty.Value.ToString(); this._stacktrace = stacktraceProperty.Value.ToString(); int eventNrParse = 0; if (int.TryParse(eventNrProperty.Value.ToString(), out eventNrParse)) { this._eventNr = eventNrParse; } else { throw new FormatException("The JObject has to be in Format {param=[{'EventNr','<Value>'},{'ExceptionType','<value>'},{'Category','<value>'},{'EventTimestamp','<value>'},{'HResult','<value>'},{'Message','<value>'},{'Stacktrace','<value>'}]}"); } DateTime eventTimestampParse = DateTime.MinValue; if (DateTime.TryParse(eventTimestampProperty.Value.ToString(), out eventTimestampParse)) { this._eventTimestamp = eventTimestampParse; } else { throw new FormatException("The JObject has to be in Format {param=[{'EventNr','<Value>'},{'ExceptionType','<value>'},{'Category','<value>'},{'EventTimestamp','<value>'},{'HResult','<value>'},{'Message','<value>'},{'Stacktrace','<value>'}]}"); } int hResultParse = 0; if (int.TryParse(hResultProperty.Value.ToString(), out hResultParse)) { this._hResult = hResultParse; } else { throw new FormatException("The JObject has to be in Format {param=[{'EventNr','<Value>'},{'ExceptionType','<value>'},{'Category','<value>'},{'EventTimestamp','<value>'},{'HResult','<value>'},{'Message','<value>'},{'Stacktrace','<value>'}]}"); } } else { throw new FormatException("The JObject has to be in Format {param=[{'EventNr','<Value>'},{'ExceptionType','<value>'},{'Category','<value>'},{'EventTimestamp','<value>'},{'HResult','<value>'},{'Message','<value>'},{'Stacktrace','<value>'}]}"); } }
public MonitorDataModel(JObject json) { JArray array = json.Descendants().OfType <JProperty>().First(x => x.Name == "param").Value.ToObject <JArray>(); if (array != null) { JProperty agentNrProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "AgentNr"); JProperty monitorTimestampProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "MonitorTimestamp"); JProperty objectIDProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "ObjectID"); JProperty resultProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == "Result"); this._result = resultProperty.Value.ToString(); this._objectID = objectIDProperty.Value.ToString(); int agentNr = 0; if (int.TryParse(agentNrProperty.Value.ToString(), out agentNr)) { this._agentID = agentNr; } else { throw new FormatException("The JObject has to be in Format {param=[{'AgentNr','<Value>'},{'MonitorTimestamp','<value>'},{'ObjectID','<value>'},{'Result','<value>'}]}"); } DateTime monitorTimestamp; if (DateTime.TryParse(monitorTimestampProperty.Value.ToString(), out monitorTimestamp)) { this._monitorTimestamp = monitorTimestamp; } else { throw new FormatException("The JObject has to be in Format {param=[{'AgentNr','<Value>'},{'MonitorTimestamp','<value>'},{'ObjectID','<value>'},{'Result','<value>'}]}"); } } else { throw new FormatException("The JObject has to be in Format {param=[{'AgentNr','<Value>'},{'MonitorTimestamp','<value>'},{'ObjectID','<value>'},{'Result','<value>'}]}"); } }
public static DataTable json_convertSP(string json) { JObject json_object = JObject.Parse(json); DataTable dt = new DataTable(); JArray json_array = JArray.Parse(json_object["data"].ToString()); json_array.Descendants().OfType <JProperty>() .Where(p => p.Name == "compatibility") .ToList() .ForEach(att => att.Remove()); dt = JsonConvert.DeserializeObject <DataTable>(json_array.ToString()); return(dt); }
public void Descendants() { JArray a = new JArray( 5, new JArray(1), new JArray(1, 2), new JArray(1, 2, 3) ); List <JToken> descendants = a.Descendants().ToList(); Assert.AreEqual(10, descendants.Count()); Assert.AreEqual(5, (int)descendants[0]); Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), descendants[descendants.Count - 4])); Assert.AreEqual(1, (int)descendants[descendants.Count - 3]); Assert.AreEqual(2, (int)descendants[descendants.Count - 2]); Assert.AreEqual(3, (int)descendants[descendants.Count - 1]); }
private static JToken GetArrayToken(JArray array, string name) { return(array.Descendants().FirstOrDefault(x => x.Type == JTokenType.String && x.Value <string>() == name)?.Parent); }
private static JProperty getJProperty(JArray array, string name) { JProperty agentNrProperty = array.Descendants().OfType <JProperty>().First(x => x.Name == name); return(agentNrProperty); }
public static Dictionary <string, string> GetObjectKeyValue(this JArray jArray) { return(jArray.Descendants() .Where(t => t.Type != JTokenType.Property && !t.HasValues) .ToDictionary(t => t.Path, t => t.ToString())); }
public void Descendants() { JArray a = new JArray( 5, new JArray(1), new JArray(1, 2), new JArray(1, 2, 3) ); List<JToken> descendants = a.Descendants().ToList(); Assert.AreEqual(10, descendants.Count()); Assert.AreEqual(5, (int)descendants[0]); Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), descendants[descendants.Count - 4])); Assert.AreEqual(1, (int)descendants[descendants.Count - 3]); Assert.AreEqual(2, (int)descendants[descendants.Count - 2]); Assert.AreEqual(3, (int)descendants[descendants.Count - 1]); }