I have a sample json like this :
{"'1234xxxxxx'":[{"AttributeId":"1","AttributeName":"Brand","AttributeValue":""},{"AttributeId":"2","AttributeName":"Color","AttributeValue":"Red4"},{"AttributeId":"3","AttributeName":"Size","AttributeValue":"44"},{"AttributeId":"4","AttributeName":"Resolution","AttributeValue":"Full HD"}]}
I have created a sample DataContract class like this :
[System.Runtime.Serialization.DataContract]
public class Rootobject
{
[System.Runtime.Serialization.DataMember]
public attr[] attrs { get; set; }
}
[System.Runtime.Serialization.DataContract]
public class attr
{
[System.Runtime.Serialization.DataMember]
public string AttributeId { get; set; }
[System.Runtime.Serialization.DataMember]
public string AttributeName { get; set; }
[System.Runtime.Serialization.DataMember]
public string AttributeValue { get; set; }
}
Now , I want to access the attributes using DataContractJsonSerializer and memorystream, but the problem is that the key '1234xxxxxx' in my json is dynamically generated everytime. So how should I access the attributes in my c# code?
If DataContractJsonSerializer is not a hard requirement for you, you could use Json.NET to deserialize the object:
JsonConvert.DeserializeObject<IDictionary<string, ICollection<attr>>>(jsonString);
Here data model.
public class Rootobject
{
public Attribute[] Attributes { get; set; }
}
public class Attribute
{
public string AttributeId { get; set; }
public string AttributeName { get; set; }
public string AttributeValue { get; set; }
}
here static method that finds and replaces DynamicAttributeName
public static string FixJsonDynamicProperty(string jsonString,char matchChar,string newParamName)
{
if (string.IsNullOrEmpty(jsonString)|| string.IsNullOrEmpty(newParamName))
{
return jsonString;
}
var startIndex = jsonString.IndexOf(matchChar);
var lastIndex = jsonString.IndexOf(matchChar, jsonString.IndexOf(matchChar) + 1);
if (startIndex >= 0 && lastIndex > startIndex)
{
var dynamicParamName = jsonString.Substring(startIndex, lastIndex - startIndex + 1);
return jsonString.Replace(dynamicParamName, newParamName);
}
return jsonString;
}
and this how you use it
var jsonString ="{\"'1234xxxxxx'\":[{\"AttributeId\":\"1\",\"AttributeName\":\"Brand\",\"AttributeValue\":\"\"},{\"AttributeId\":\"2\",\"AttributeName\":\"Color\",\"AttributeValue\":\"Red4\"},{\"AttributeId\":\"3\",\"AttributeName\":\"Size\",\"AttributeValue\":\"44\"},{\"AttributeId\":\"4\",\"AttributeName\":\"Resolution\",\"AttributeValue\":\"Full HD\"}]}";
var newJsonString = FixJsonDynamicProperty(jsonString,'\'',nameof(Rootobject.Attributes));
Console.WriteLine(newJsonString);
you can deserialize newJsonStringwithout problem. New PropertyName matches in RootObject.Attributes . Also FixJsonDynamicProperty can be simplified with RegExp .
Related
I am still learning how to use LINQ and now I'm struggling with this situation.
I have this XML file (example)
<Results>
<PrxComissao>
<Id>0</Id>
<NumErro>0</NumErro>
<RetCode>0</RetCode>
<IdEvento>0</IdEvento>
<ExecutionTimeMilliseconds>63596143450994.227</ExecutionTimeMilliseconds>
<ExecutionTimeSeconds>63596143450.994225</ExecutionTimeSeconds>
<CodComissao>CFE</CodComissao>
<Montante>20.00</Montante>
<Percentagem>0.0000</Percentagem>
<MntMin>0.00</MntMin>
<MntMax>0.00</MntMax>
<Nome>CFE</Nome>
<Descricao>Custo Factura Estrangeiro</Descricao>
</PrxComissao>
<PrxComissao>
<Id>0</Id>
<NumErro>0</NumErro>
<RetCode>0</RetCode>
<IdEvento>0</IdEvento>
<ExecutionTimeMilliseconds>63596143450994.227</ExecutionTimeMilliseconds>
<ExecutionTimeSeconds>63596143450.994225</ExecutionTimeSeconds>
<CodComissao>CFE</CodComissao>
<Montante>20.00</Montante>
<Percentagem>0.0000</Percentagem>
<MntMin>13.00</MntMin>
<MntMax>123.00</MntMax>
<Nome>CFE</Nome>
<Descricao>www</Descricao>
</PrxComissao>
</Results>
And now what I want to do is get all the XML elements inside the "PrxComissao", and then assign them to my class. This is the code I was trying
XDocument xDoc = XDocument.Parse(resultado);
List<PrxComissao> lstPrxComissao = xDoc.Elements("Results")
.Elements("PrxComissao")
.Elements()
.Select(BL_CartaoCredito.Utils.Data.Converter.FromXElement<PrxComissao>)
.ToList();
ObjAuxResult = lstPrxComissao;
What I am trying to do with this Converter.FromXElement<PrxComissao> is get all that elements and assign them.
Here is my class
public class PrxComissao
{
public string CodComissao { get; set; }
public string Montante { get; set; }
public string Percentagem { get; set; }
public string MntMin { get; set; }
public string MntMax { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public string TipoImposto { get; set; }
public string ComFinanciamento { get; set; }
public string iActivo { get; set; }
public string UtlModificacao { get; set; }
public string DtModificacao { get; set; }
public string UtlCriacao { get; set; }
public string DtCriacao { get; set; }
}
public static T FromXElement<T>(XElement element) where T : class, new()
{
T value = new T();
foreach (var subElement in element.Elements())
{
var field = typeof(T).GetField(subElement.Name.LocalName);
field.SetValue(value, (string)subElement);
}
return value;
}
So now I have two problems. First, I can't get to the elements inside PrxComissao always returns me nothing and then is my LINQ Select correct ? Or is there a better way ?
Start with the Descendants assuming your convertor takes an XElement:
List<PrxComissao> lstPrxComissao = xDoc.Descendants()
.Elements("PrxComissao")
.Select(el => BL_CartaoCredito.Utils.Data.Converter.FromXElement<PrxComissao>(el))
.ToList();
and then (untested) ...
public static T FromXElement<T>(XElement element) where T : class, new()
{
var typeOfT = typeof(T);
T value = new T();
foreach (var subElement in element.Elements())
{
var prop = typeOfT.GetProperty(subElement.Name.LocalName);
if(prop != null)
{
prop.SetValue(value, subElement.Value);
}
}
return value;
}
Currently, your code passes individual child elements of <PrxComissao> to the converter method. I believe you want to pass XElement that references <PrxComissao> instead :
List<PrxComissao> lstPrxComissao =
xDoc.Elements("Results")
.Elements("PrxComissao")
.Select(o => BL_CartaoCredito.Utils
.Data
.Converter
.FromXElement<PrxComissao>(o)
)
.ToList();
Besides, your class uses properties instead of field, so the corresponding reflection method supposed to be used here is GetProperty(), not GetField().
I am querying Firebase and retrieve a collection of objects like so:
{"-K5f0ccEKkVkxTAavQKY": {
"Appeal": {
"ID": "1450273330435",
"comps": [
162248,
162272,
162273,
162281,
162544
],
"property": {
"Address": "15 Main Street",
"propID": 169729
},
"timeDateStamp": "Wed Dec 16 2015 08:42:10 GMT-0500 (Eastern Standard Time)",
"userUUID": "google:229139952703238437512",
"year": 2016
}
}}
I would like to deserialize them into objects with this definition:
public class Appeal
{
public string ID;
public List<string> comps;
public AppealProperty property;
public string timeDateStamp;
public string UUID;
public int year;
}
public class AppealProperty
{
public string address;
public string propID;
}
I have troubles getting it deserialized. I don't need the initial string (e.g. "K5f0ccEKkVkxTAavQKY"). I'm able to change the object definitions if need be. I have a feeling a Dictionary would be useful.
The quick and dirty object is to use Dictionary<string,Appeal> as your deserialization target. At that point it would be as simple as:
var firebaseLookup = JsonConvert.DeserializeObject<Dictionary<string,Appeal>>(json);
var data = firebaseLookup.Values.ToList(); // or FirstOrDefault();
This approach would also handle the case if you ever had to get multiple objects at once, and it would give you the opportunity to use that key if it turns out the key was important after all.
You could serialise your data into the classes below.
public class AppealProperty
{
public string Address { get; set; }
public int propID { get; set; }
}
public class Appeal
{
public string ID { get; set; }
public List<int> comps { get; set; }
public AppealProperty property { get; set; }
public string timeDateStamp { get; set; }
public string userUUID { get; set; }
public int year { get; set; }
}
public class FireBase
{
public Appeal Appeal { get; set; }
}
public class RootObject
{
[JsonProperty(PropertyName = " - K5f0ccEKkVkxTAavQKY")]
public FireBase FireBaseRoot
{
get;
set;
}
}
Assuming that you are using JSON.NET, you can then get the object you are after, using this snippet:
var firebaseObject = JsonConvert.DeserializeObject<RootObject>(json);
var data = firebaseObject.FireBaseRoot.Appeal;
If the root name is dynamic, as indicated by your comment, you could skip the root instead and serialise straight into the FireBase class:
JObject parsedJson = JObject.Parse(json);
var fireBase = parsedJson.First.Children().First().ToObject(typeof (FireBase));
Since I've never been able to parse a DataSnapshot with newtonSoft Json parser, I did this to build a list of object I needed to put in a ListView:
MyModelObject class
public class MyModelObject: Java.Lang.Object
{
public string Title { get; set; }
public string Description { get; set; }
public MyModelObject(){}
}
into My Listener
public void OnDataChange(DataSnapshot snapshot)
{
List<MyModelObjecct> myList = new List<MyModelObject>();
myList = databaseService
.GetMyModelObjectList(snapshot
.Children?
.ToEnumerable<DataSnapshot>());
}
Method into the DatabaseService class
public List<MyModelObject> GetMyModelObjectList(IEnumerable<DataSnapshot> enumerableSnapshot)
{
List<MyModelObject> list = new List<MyModelObject>();
foreach (var item in enumerableSnapshot)
{
list.Add(ObjectExtensions.DataSnapshotToObject<MyModelObject>(item.Children?.ToEnumerable<DataSnapshot>()));
}
return list;
}
ObjectExtensions class
public static class ObjectExtensions
{
public static T DataSnapshotToObject<T>(IEnumerable<DataSnapshot> source)
where T : class, new()
{
var someObject = new T();
var someObjectType = someObject.GetType();
foreach (var item in source)
{
someObjectType
.GetProperty(item.Key)
.SetValue(someObject, item.Value.ToString(), null);
}
return someObject;
}
}
I have a Yaml file:
https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/src/icons.yml
And a class:
public class IconSearch
{
public string Name { get; set; }
public string ClassName { get; set; }
public IEnumerable<string> Filters { get; set; }
}
Can you tell me how I can deserialize the yaml to an IEnumerable of objects?
I expect something like this to work, but it returns null - I'm guessing it's because one of my properties is not the root node (icons). Instead, I'm trying to serialize the children of the root?
var input = new StringReader(reply);
var yaml = new YamlStream();
yaml.Load(input);
var icons = deserializer.Deserialize<IconSearch>(input);
The class you are trying to deserialize to seems to be missing properties.
I went the round about way of converting yaml to json to csharp and this is class that was generated:
public class Rootobject
{
public Icon[] icons { get; set; }
}
public class Icon
{
public string[] categories { get; set; }
public object created { get; set; }
public string[] filter { get; set; }
public string id { get; set; }
public string name { get; set; }
public string unicode { get; set; }
public string[] aliases { get; set; }
public string[] label { get; set; }
public string[] code { get; set; }
public string url { get; set; }
}
Resources used :
YAML to JSON online
JSON to CSHARP (I used Paste special in visual studio)
Use this to deserialize
var icons = deserializer.Deserialize<RootObject>(input);
Update
I have commented out the line that you use to create YamlStream as it is not required (it positions the reader to the end of the stream instead of the beginning, which would explain why you were getting null earlier). Your main method looks as follows and works. I have also fixed the bug that Antoine mentioned
public static void Main()
{
string filePath = "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/src/icons.yml";
WebClient client = new WebClient();
string reply = client.DownloadString(filePath);
var input = new StringReader(reply);
//var yamlStream = new YamlStream();
//yamlStream.Load(input);
Deserializer deserializer = new Deserializer();
//var icons = deserializer.Deserialize<IconSearch>(input);
//Testing my own implementation
//if (icons == null)
// Console.WriteLine("Icons is null");
//Testing Shekhar's suggestion
var root = deserializer.Deserialize<Rootobject>(input);
if (root == null)
Console.WriteLine("Root is null");
}
here's my class
[DataContract]
public class WytypowaneMecze
{
public WytypowaneMecze() { }
public WytypowaneMecze(String data, String d_gospodarzy, String d_gosci, String wynik)
{
this.Data = data;
this.D_gospodarzy = d_gospodarzy;
this.D_gosci = d_gosci;
this.Wynik = wynik;
}
public string Data { get; set; }
public string D_gospodarzy { get; set; }
public string D_gosci { get; set; }
public string Wynik { get; set; }
}
}
that's how i write to file my list wytypowane
private async void zapiszPlik()
{
string json = "wytypowane.json";
var serializer = new DataContractJsonSerializer(typeof(List<WytypowaneMecze>));
var stream = await Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForWriteAsync(json, CreationCollisionOption.OpenIfExists);
using (stream)
{
serializer.WriteObject(stream, wytypowane);
}
}
but i can't read this...
Additional information: '{}][{},{}][{}][{}][{},{}][{}][{}][{}][{}][{}][{}][{}]' is not a valid JSON primitive. This error can also occur when extraneous data is present after the JSON data.
private async void odczyt()
{
string json = "wytypowane.json";
List<WytypowaneMecze> lista = new List<WytypowaneMecze>();
var deserializer = new DataContractJsonSerializer(typeof(List<WytypowaneMecze>));
var stream = await Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForReadAsync(json);
using (stream)
{
lista = (List<WytypowaneMecze>)deserializer.ReadObject(stream);
}
}
You need to mark the properties you want to serialize with the DataMember attribute. That's because you are using the DataContractJsonSerializer and data contracts are opt-in:
Apply the DataMemberAttribute attribute in conjunction with the DataContractAttribute to identify members of a type that are part of a data contract. One of the serializers that can serialize data contracts is the DataContractSerializer.
The data contract model is an "opt-in" model. Applying the DataMemberAttribute to a field or property explicitly specifies that the member value will be serialized. In contrast, the BinaryFormatter serializes public and private fields of a type, and the XmlSerializer serializes only public fields and properties of a type.
Thus:
[DataContract]
public class WytypowaneMecze
{
public WytypowaneMecze() { }
public WytypowaneMecze(String data, String d_gospodarzy, String d_gosci, String wynik)
{
this.Data = data;
this.D_gospodarzy = d_gospodarzy;
this.D_gosci = d_gosci;
this.Wynik = wynik;
}
[DataMember]
public string Data { get; set; }
[DataMember]
public string D_gospodarzy { get; set; }
[DataMember]
public string D_gosci { get; set; }
[DataMember]
public string Wynik { get; set; }
}
This is a very noob question, but I have been googling and can't seem to work out the solution myself.
I have created a class that has a number of fields (below). I am grabbing data from a .JSON file.
public class WeatherData
{
//WeatherDatas
public string airtemp { get; set; }
public string apparenttemp { get; set; }
public string windspeedkph { get; set; }
public string windgustskph { get; set; }
public string humidity { get; set; }
public string dewpoint { get; set; }
public string deltaT { get; set; }
public string pressure { get; set; }
public WeatherData(string json, int index)
{
JObject jsonObject = JObject.Parse(json);
JToken jObbs = jsonObject["observations"];
JToken jData = jObbs["data"];
airtemp = (string)jData[index]["air_temp"];
apparenttemp = (string)jData[index]["apparent_t"];
windspeedkph = (string)jData[index]["wind_spd_kmh"];
windgustskph = (string)jData[index]["gust_kmh"];
humidity = (string)jData[index]["rel_hum"];
pressure = (string)jData[index]["press_qnh"];
}
}
Using the above I can get the "airtemp" from "WeatherData.airtemp". But due to some bells and whistles I want to add later what I really want to do is return not just the airtemp value but a field/property indicating the type of the value. For example something like:
WeatherData.WeatherDatas.airtemp.value & WeatherData.WeatherDatas.airtemp.type
Where .value would be air temp and .type be the string "airtemp".
I just can't seem to work out how to describe to google what I am trying to do.
I think you may want to look into using a dictionary. Dictionaries in c# use Key, Value pairs, so you may wish to create a dictionary of Your key values would be the type, such as airtemp and the value values would be the value such as 32.54.
I recommend looking up this page if you're new to C# and want to learn Dictionaries, or any other cool C# things. http://www.dotnetperls.com/dictionary
What you can do here is to use a struct to represent your instance attributes. S.t like this:
public struct Data
{
public Type type; // if you want a string type (I don't know why) you can use a string type
public string Value;
}
Then you class will look like this:
public class WeatherData
{
//WeatherDatas
public Data airtemp { get; set; }
...
public WeatherData(string json, int index)
{
JObject jsonObject = JObject.Parse(json);
JToken jObbs = jsonObject["observations"];
JToken jData = jObbs["data"];
airtemp.Value = new Data((string)jData[index]["air_temp"], typeof(string));
...
}
}
Note that: If you have too many properties in your class, consider using a class istead of struct.
You need to explore types:
Type myType;
Object windy = "strong";
myType = windy.GetType();
Then use typeof() to see what you've got.
Not sure that dictionary is the way forward.
You can get property name without change your structure:
by example: PropertyUtil<WeatherData>.GetName(x => x.airtemp);
https://stackoverflow.com/a/6043028/440030
(source code)
public static class PropertyUtil<TSource>
{
public static string GetPropertyName<TResult>(
Expression<Func<TSource, TResult>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
}
You can create your own data type, instead of using string use something else Like;
public class UserString
{
public Type _type{ get; set; }
public string value{ get; set; }
public UserString()
{
_type = null;
value = string.Empty;
}
}
public class WeatherData
{
//WeatherDatas
public UserString airtemp { get; set; }
public UserString apparenttemp { get; set; }
public UserString windspeedkph { get; set; }
public UserString windgustskph { get; set; }
public UserString humidity { get; set; }
public UserString dewpoint { get; set; }
public UserString deltaT { get; set; }
public UserString pressure { get; set; }
public WeatherData(string json, int index)
{
JObject jsonObject = JObject.Parse(json);
JToken jObbs = jsonObject["observations"];
JToken jData = jObbs["data"];
airtemp.value = (string)jData[index]["air_temp"];
// airtemp._type = typeof(string); //Something like that.
apparenttemp = (string)jData[index]["apparent_t"];
windspeedkph = (string)jData[index]["wind_spd_kmh"];
windgustskph = (string)jData[index]["gust_kmh"];
humidity = (string)jData[index]["rel_hum"];
pressure = (string)jData[index]["press_qnh"];
}
}