I have a JSON content like:
{
"ABCD1":[{"gopName":"JHIKJUS"},{"gopName":"LKKJHGYT"}],
"CBFD1":[{"gopName":"HT"},{"gopName":"OP"}]
}
I have created one custom Class i.e.
public class DeskGopMapper
{
public List<string> GopName { get; set; }
public string DeskName { get; set; }
}
Need to know how can we write a custom parser so that it should not be tightly coupled.
Have tried something like:
class Program
{
static void Main(string[] args)
{
using (var stream = File.OpenRead(#"sample.txt"))
using (var reader = new StreamReader(stream))
{
var line = reader.ReadToEnd();
var rawObj = JObject.Parse(line);
List<DeskGopMapper> map = new List<DeskGopMapper>();
foreach (var obj in rawObj)
{
var m = new DeskGopMapper {DeskName = obj.Key, GopName = new List<string>()};
foreach (var prop in obj.Value)
{
m.GopName.Add(prop["gopName"].ToString());
}
map.Add(m);
}
}
}
}
Though i am not very much convinced with way of parsing.
What is the better way to achieve Parsing the JSON response into List<DeskGopMapper> ?
You can use Json.Net's LINQ-to-JSON API to simplify your code:
string json = File.ReadAllText(#"sample.txt");
List<DeskGopMapper> map = JObject.Parse(json)
.Properties()
.Select(p => new DeskGopMapper
{
DeskName = p.Name,
GopName = p.Value.Children<JObject>()
.Select(j => (string)j["gopName"])
.ToList()
})
.ToList();
Demo fiddle: https://dotnetfiddle.net/0d2ZzH
Related
I'm using csvHelper (version 2.8.4) to write a class to csv.
My class looks like this:
public class classA
{
public int Amount { get; set; }
public Dictionary<string, string> Dict{ get; set; }
}
Is it possible to write a mapper that maps Dict property to multiple columns? using some sort of converter?
for example if the class has the values:
Amount = 15
Dict = new Dictionary<string,string>{["a1"] = "a2",["b1"] = "b2"}
I want the resulting csv to be:
Amount,a1,b1
15,a2,b2
Thanks!
Possibly the easiest way is going to be to manually write out the dictionary part.
*** Update to work with CsvHelper Version 2.8.4 ***
void Main()
{
var records = new List<classA>
{
new classA {
Amount = 15,
Dict = new Dictionary<string,string>{["a1"] = "a2",["b1"] = "b2"}
}
};
using (var csv = new CsvWriter(Console.Out))
{
var dict = records.First().Dict;
var properties = typeof(classA).GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.Name != "Dict")
{
csv.WriteField(property.Name);
}
}
foreach (var item in dict)
{
csv.WriteField(item.Key);
}
csv.NextRecord();
foreach (var record in records)
{
foreach (PropertyInfo property in properties)
{
if (property.Name != "Dict")
{
csv.WriteField(property.GetValue(record));
}
}
foreach (var item in record.Dict)
{
csv.WriteField(item.Value);
}
csv.NextRecord();
}
}
}
// You can define other methods, fields, classes and namespaces here
public class classA
{
public int Amount { get; set; }
public Dictionary<string, string> Dict { get; set; }
}
*** Works for current Version 27.2.1 ***
void Main()
{
var records = new List<classA>
{
new classA { Amount = 15, Dict = new Dictionary<string,string>{["a1"] = "a2",["b1"] = "b2"} },
};
using (var csv = new CsvWriter(Console.Out, CultureInfo.InvariantCulture))
{
var dict = records.First().Dict;
csv.WriteHeader<classA>();
foreach (var item in dict)
{
csv.WriteField(item.Key);
}
csv.NextRecord();
foreach (var record in records)
{
csv.WriteRecord(record);
foreach (var item in record.Dict)
{
csv.WriteField(item.Value);
}
csv.NextRecord();
}
}
}
public class classA
{
public int Amount { get; set; }
public Dictionary<string, string> Dict { get; set; }
}
As mentioned in linked question, you may use ExpandoObject to serialize dictionary.
The following code will work for writing to CSV only, it's converting classA objects to ExpandoObject during serialization, including Amount property which is added manually.
public static List<dynamic> ToExpandoObjects(IReadOnlyList<classA> aObjects)
{
var allKeys = aObjects
.SelectMany(a => a.Dict.Keys)
.Distinct()
.ToHashSet();
var result = new List<dynamic>();
foreach (var a in aObjects)
{
var asExpando = new ExpandoObject();
var asDictionary = (IDictionary<string, object>)asExpando;
asDictionary[nameof(classA.Amount)] = a.Amount;
foreach (var key in allKeys)
{
if(a.Dict.TryGetValue(key, out var value))
asDictionary[key] = value;
else
asDictionary[key] = null;
}
result.Add(asExpando);
}
return result;
}
...
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(ToExpandoObjects(records));
}
E.g. called as:
var records = new[] {
new classA
{
Amount = 15,
Dict = new Dictionary<string,string>{["a1"] = "a2",["b1"] = "b2"}
},
new classA
{
Amount = 15,
Dict = new Dictionary<string,string>{["c1"] = "c2",["b1"] = "b2"}
}
};
StringBuilder sb = new StringBuilder();
using (var writer = new StringWriter(sb))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(ToExpandoObjects(records));
}
Console.WriteLine(sb.ToString());
produces
Amount
a1
b1
c1
15
a2
b2
15
b2
c2
I want to write a string into one Column in an .csv (Excel) file. My Problem is that the string is written into multiple Columns.
In this screenshot for example I have 20 Columns.
GetMetadataCompleteResponse resultValue = null;
string jsonData = null;
await Task.Run(() =>
{
byte[] rawData = Convert.FromBase64String(responseContent);
jsonData = CompressUtil.Unzip(rawData);
});
resultValue = JsonConvert.DeserializeObject<GetMetadataCompleteResponse>(jsonData);
foreach(string a in resultValue.Value.Values)
{
foreal += a;
}
await Log.Info("callWebservice for " + strUrl + ", Result: " + objErrorDetails.Code + ", " + foreal);
edit
I've noticed that the new Column starts after every ';'(semicolon). I probably can just replace it with something else.
I think you have 2 issues. The first one is how you write your CSV with simple string concatenation. With no escaping or double quote.
The Json will have commas , that will be separator in your CSV.
In order to produc e a valid CSV you should read the RFC 4180 and use a proper library to handle the Serialisation.
Here is an Minimal, Complete, and Verifiable example of writing a Json in a CSV column.
using CsvHelper;
using CsvHelper.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public class Program
{
public static void Main()
{
var input = new Foo
{
Label = "My Foo",
Bars = new List<Bar> {
new Bar{Label="Bar2"},
new Bar{Label="Bar1"},
new Bar{Label="Bar3"},
}
};
var json = JsonConvert.SerializeObject(input);
var myObject = new CsvObject
{
Label = "My CSV object",
FooString = json,
};
var result = "";
// Writing into a string instead of a file for debug purpuse.
using (var stream = new MemoryStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
using (var csv = new CsvWriter(writer))
{
csv.Configuration.RegisterClassMap<CsvObjectMap>();
csv.WriteHeader<CsvObject>();
csv.NextRecord();
csv.WriteRecord(myObject);
csv.NextRecord();
writer.Flush();
stream.Position = 0;
result = reader.ReadToEnd();
}
Console.WriteLine(result);
}
private sealed class CsvObjectMap : ClassMap<CsvObject>
{
public CsvObjectMap()
{
Map( m => m.FooString );
Map( m => m.Label );
}
}
public class CsvObject
{
public string Label { get; set; }
public string FooString { get; set; }
}
public class Foo
{
public string Label { get; set; }
public List<Bar> Bars { get; set; }
}
public class Bar
{
public string Label { get; set; }
}
}
Live demo : https://dotnetfiddle.net/SNqZX1
In this exemple I have used CsvHelper for CSV serialisation, and Json.NET for the Json serialisation. Note that Writing a CSV to a file is a more simlpe task that to a string like in this example
I do a request to a api that I'm using and this is the response that I get back.
{ "id": 1139, "performanceStatus": "OK", "availabilityStatus": "OK" }
I would like to convert this response in a list where I then can use a for/foreach loop and later can use linq and SelectMany to create a whole new list.
I got this far, but I am stuck the code is not hitting the var "newJson"..
Can someone help me out?
var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
dynamic parsedJson = JObject.Parse(json);
foreach (var j in parsedJson)
{
j.Replace(JObject.FromObject(
new {
id = j.id,
performance = j.performanceStatus,
availability = j.availabilityStatus
}));
}
var newJson = parsedJson.ToString();
Later I would like to Deserialize it into a strongly typed class. Like so
boi = await Task.Run(() => JsonConvert.DeserializeObject<Boi>(newJson)).ConfigureAwait(false);
Here is the strongly typed class
public class Boi
{
public int Id { get; set; }
public string PerformanceStatus { get; set; }
public string AvailabilityStatus { get; set; }
}
public class NewBoi
{
public List<Boi> eeg { get; set; }
}
You could do this:
var jsn = "{ \"id\": 1139, \"performanceStatus\": \"OK\", \"availabilityStatus\": \"OK\" }";
var bois = new List<Boi> { JsonConvert.DeserializeObject<Boi>(jsn) };
var newBoi = new NewBoi() { eeg = bois };
Although I would question the need for the NewBoi class - you could just work with the List.
To deserialize to a well known type use
JsonConvert.Deserialize<T>(jsonString,new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
where T is your well known type.
I have JSON that looks like this:
{
"MobileSiteContents": {
"au/en": [
"http://www.url1.com",
"http://www.url2.com",
],
"cn/zh": [
"http://www.url2643.com",
]
}
}
I'm trying to deserialize it into an IEnumerable of classes that look like this:
public class MobileSiteContentsContentSectionItem : ContentSectionItem
{
public string[] Urls { get; set; }
}
public abstract class ContentSectionItem
{
public string Culture { get; set; }
}
Is that possible?
I realise I will probably need to use a Custom JsonConverter for this, but can't find any examples.
I started writing a method to convert using JObject.Parse but not sure if this is the correct / most efficient route to go down:
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
{
var jobject = JObject.Parse(json);
var result = new List<MobileSiteContentsContentSectionItem>();
foreach (var item in jobject.Children())
{
var culture = item.Path;
string[] urls = new[] { "" }; //= this is the part I'm having troble with here...
result.Add(new MobileSiteContentsContentSectionItem { Culture = culture, Urls = urls });
}
return result;
}
You're on the right track. Here are the corrections you need to make:
You're iterating over children of the top-level object expecting to get data that is actually in an object one level further down. You need to navigate to the value of the MobileSiteContents property and iterate over the children of that.
When you take the Children() of the JObject, use the overload that lets you cast them to JProperty objects; that will make it much easier to extract the data you want.
Get the culture from the Name of the JProperty item
To get the urls, get the Value of the JProperty item and use ToObject<string[]>() to convert it to a string array.
Here is the corrected code:
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
{
var jObject = JObject.Parse(json);
var result = new List<MobileSiteContentsContentSectionItem>();
foreach (var item in jObject["MobileSiteContents"].Children<JProperty>())
{
var culture = item.Name;
string[] urls = item.Value.ToObject<string[]>();
result.Add(new MobileSiteContentsContentSectionItem { Culture = culture, Urls = urls });
}
return result;
}
If you like terse code, you can reduce this to a "one-liner":
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
{
return JObject.Parse(json)["MobileSiteContents"]
.Children<JProperty>()
.Select(prop => new MobileSiteContentsContentSectionItem
{
Culture = prop.Name,
Urls = prop.Value.ToObject<string[]>()
})
.ToList();
}
Demo:
class Program
{
static void Main(string[] args)
{
string json = #"
{
""MobileSiteContents"": {
""au/en"": [
""http://www.url1.com"",
""http://www.url2.com"",
],
""cn/zh"": [
""http://www.url2643.com"",
]
}
}";
foreach (MobileSiteContentsContentSectionItem item in Parse(json))
{
Console.WriteLine(item.Culture);
foreach (string url in item.Urls)
{
Console.WriteLine(" " + url);
}
}
}
public static IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
{
return JObject.Parse(json)["MobileSiteContents"]
.Children<JProperty>()
.Select(prop => new MobileSiteContentsContentSectionItem()
{
Culture = prop.Name,
Urls = prop.Value.ToObject<string[]>()
})
.ToList();
}
public class MobileSiteContentsContentSectionItem : ContentSectionItem
{
public string[] Urls { get; set; }
}
public abstract class ContentSectionItem
{
public string Culture { get; set; }
}
}
Output:
au/en
http://www.url1.com
http://www.url2.com
cn/zh
http://www.url2643.com
I tried this using Json.Net and works fine.
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json)
{
dynamic jobject = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
var result = new List<MobileSiteContentsContentSectionItem>();
var urls = new List<string>();
foreach (var item in jobject.MobileSiteContents)
{
var culture = item.Name;
foreach(var url in item.Value)
urls.Add(url.Value);
result.Add(new MobileSiteContentsContentSectionItem { Culture = culture, Urls = urls.ToArray() });
}
return result;
}
I have a
var list = new List<MyStructure>();
I want to extract MyStructure.Foo and MyStructure.Bar and put these two in a NameValueCollection. I want NameValueCollection because I am using WebClient.UploadValues()
The following creates an IEnumerable<NamedValueCollection>. I need just a NameValueCollection
var nameValueCollection = structure.Select(x => new NameValueCollection
{
{ x.TypeId.ToString(), x.Value }
});
Thanks.
No need to use LINQ here
foreach(var myStruct in list)
{
myNameValueCollection.Add(myStruct.Foo, myStruct.Bar);
}
Try the following.
class MyStructure
{
public string Foo { get; set; }
public string Bar { get; set; }
}
public static void Main(string[] args)
{
var list = new List<MyStructure>();
NameValueCollection nameValueCollection = new NameValueCollection();
list.ForEach(x => {
nameValueCollection.Add(x.Foo, x.Bar);
});
}