public static void getsynonyms(string word, int id) { Console.WriteLine(word); //List<string> synonymsfromdiction = new List<string>(); var client1 = new HttpClient(); client1.DefaultRequestHeaders.Accept.Clear(); client1.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); client1.DefaultRequestHeaders.Add("app_id", "98d8def0"); client1.DefaultRequestHeaders.Add("app_key", "5b8263cc08b590ecafffdfd213e39704"); var response = client1.GetAsync("https://od-api.oxforddictionaries.com/api/v1/entries/en/" + word + "/synonyms").Result; string content = response.Content.ReadAsStringAsync().Result; Console.WriteLine(content); if (isValidJSON(content) == true) { Console.WriteLine("Json"); jsonToCSV(content, id); } else { Console.WriteLine("word without synonym {0}", word); Console.WriteLine("HTML"); graphdb elsewordgraph = new graphdb(); elsewordgraph.csvtographdbfunction(id, word); } }
public static void jsonToCSV(string jsonContent, int id) { var obj = JObject.Parse(jsonContent); // Collect column titles: all property names whose values are of type JValue, distinct, in order of encountering them. var values = obj.DescendantsAndSelf() .OfType <JProperty>() .Where(p => p.Value is JValue) .GroupBy(p => p.Name) .ToList(); var columns = values.Select(g => g.Key).ToArray(); // Filter JObjects that have child objects that have values. var parentsWithChildren = values.SelectMany(g => g).SelectMany(v => v.AncestorsAndSelf().OfType <JObject>().Skip(1)).ToHashSet(); // Collect all data rows: for every object, go through the column titles and get the value of that property in the closest ancestor or self that has a value of that name. var rows = obj .DescendantsAndSelf() .OfType <JObject>() .Where(o => o.PropertyValues().OfType <JValue>().Any()) .Where(o => o == obj || !parentsWithChildren.Contains(o)) // Show a row for the root object + objects that have no children. .Select(o => columns.Select(c => o.AncestorsAndSelf() .OfType <JObject>() .Select(parent => parent[c]) .Where(v => v is JValue) .Select(v => (string)v) .FirstOrDefault()) .Reverse() // Trim trailing nulls .SkipWhile(s => s == null) .Reverse()); // Convert to CSV var csvRows = new[] { columns }.Concat(rows).Select(r => string.Join(",", r)); var csv = string.Join("\n", csvRows); Console.WriteLine(csv); //File.Delete(@"C:\Users\Administrator\Documents\Neo4j\default.graphdb\import\export.csv"); //File.SetAttributes("../../../../../home/neo4j/import/export.csv", FileAttributes.Normal); File.WriteAllText("../../../../../home/neo4j/import/export.csv", csv); File.SetAttributes("../../../../../home/neo4j/import/export.csv", FileAttributes.Normal); //File.WriteAllText(@"C:\Users\Administrator\Documents\Neo4j\default.graphdb\import\export.csv", csv); graphdb objgraphdb = new graphdb(); objgraphdb.csvtographdbfunction(id); }