How to create response from dictionary? - c#

I am trying to create below response from dictionary:
['Employee1'] : List of skills
Code :
public class Skills
{
public string Skill {get;set;}
}
var skills=FetchSkills();
var dictionary = new Dictionary<string, List<Skills>>();
dictionary.Add('Employee1',skills);
Now i am trying to create below response:
'Employee1' =
{
{"skill":"skill1"},{"skill":"skill2"},{"skill":"skill3"}
}
I want skill property in camel case in my final response.
This is how i am trying to create response but not getting how to create expected response:
return Json(dictionary.Select
(
), JsonRequestBehavior.AllowGet);

Given class
public class Skills {
[JsonProperty("skill")]
public string Skill {get;set;}
}
and used like this
var skills=FetchSkills();
var dictionary = new Dictionary<string, List<Skills>>();
dictionary.Add('Employee1',skills);
return Json(dictionary, JsonRequestBehavior.AllowGet);
should produce
{
"Employee1":[
{"skill":"skill1"},{"skill":"skill2"},{"skill":"skill3"}
]
}

Related

Using JArray for serializing data from database in WebAPI (C#)

I want to achieve the following JSON data:
[
{
"name":"Deutschland",
"code":"de"
},
{
"name":"Frankreich",
"code":"fr"
},
{
"name":"Japan",
"code":"jpn"
}
]
Currently I'm getting this result of JSON data:
{
"groups":[
{
"name":"Deutschland",
"code":"de"
},
{
"name":"Frankreich",
"code":"fr"
},
{
"name":"Japan",
"code":"jpn"
}
]
}
Here is the code of the Controller:
public dynamic GetGroups()
{
JObject o = JObject.FromObject(new
{
groups = from g in db.QR_Groups
select new
{
name = g.name,
code = g.code
}
});
return o;
/*Here I've tried to get JSON data as array without the Property "groups"*/
//JArray a = new JArray(
// from g in db.QR_Groups
// select new JValue(g));
//return a;
}
Can anyone tell me how to retrieve the JSON data as per the first JSON example above?
And is the type "dynamic" good practice for the method?
First of all there is no need to do serialization manually. ASP.Net WebApi MediaFormatters are going to take care of it based on the Request Content-Type. So Create a class as shown below.
public class Group
{
public string name { get; set; }
public string code { get; set; }
}
Then your Web API endpoint should be -
[HttpGet]
public HttpResponseMessage GetCountries()
{
List<Group> groups = (from g in db.QR_Groups
select new Group
{
name = g.name,
code = g.code
}).ToList();
return Request.CreateResponse(HttpStatusCode.OK, groups);
}
And when I make a Fiddler request, I was able to get the output which you are interested -
Try this one:
var json = JsonConvert.SerializeObject(from g in db.QR_Groups
select new
{
name = g.name,
code = g.code
});
And is the type "dynamic" good practice for the method?
no, it's not best practise. Better one is to create new class

How to retrieve a property from an object within a dictionary in a MongoDB document?

Our Mongo data looks like this:
{
"_id" : ObjectId("542d881b8bc641bbee1f8509"),
"ExtendedProperties" : {
"Context" : {
"_t" : "LoggingContext",
"DeclaringTypeName" : "EndpointConfig"
}
}
}
In the C# code, the ExtendedProperties are represented as follows:
public class LogEntry
{
public IDictionary<string, object> ExtendedProperties { get; set; }
}
I have tried every method I can find to be able to query against the value of DeclaringTypeName. Nothing seems to work, as shown in the following code:
// This throws an UnsupportedOperationException with the following message:
// Unable to determine the serialization information for the expression: (LogEntry e) => e.ExtendedProperties.get_Item("DeclaringTypeName").ToString().
query.Add(Query<LogEntry>.EQ(e => ((LoggingContext)e.ExtendedProperties["Context"]), this.DeclaringTypeName ));
// This returns zero matching rows:
query.Add(Query.EQ("ExtendedProperties.Context.DeclaringTypeName", this.DeclaringTypeName));
// This returns zero matching rows:
query.Add(Query.ElemMatch("ExtendedProperties.Context", Query.EQ("DeclaringTypeName", this.DeclaringTypeName)));
// This reports that ExtendedProperties must implement a specific interface and must not return null:
query.Add(Query<LogEntry>.ElemMatch(e => e.ExtendedProperties, qb => Query.EQ("Context.DeclaringTypeName", this.DeclaringTypeName)));
For clarity, I have researched every StackOverflow, CodePlex, and Mongo.org thread I can find, and have as yet been unable to resolve this correctly.
Naturally, it's going to be something I'm doing wrong.
Someone please throw me a bone.
I defined the LogEntry class as
public class LogEntry
{
public ObjectId Id { get; set; }
public IDictionary<string, object> ExtendedProperties { get; set; }
}
then I inserted the sample document by
var log = new LogEntry
{
ExtendedProperties = new Dictionary<string, object>
{
{
"Context", new LoggingContext
{
DeclaringTypeName = "EndpointConfig"
}
}
}
};
collection.Insert(log);
then I performed the query by:
var rawQuery = Query.EQ("ExtendedProperties.Context.DeclaringTypeName", "EndpointConfig");
var query = new List<IMongoQuery>();
query.Add(rawQuery);
var rawResult = collection.Find(rawQuery).ToList();
the query will send mongo below query
db.messages.find({ "ExtendedProperties.Context.DeclaringTypeName" : "EndpointConfig" })
And I got the result

Deserialize Json with no name Fields and Format string, array

I have the following json object:
[
"sd",
[
"sdg\u0026e",
"sdlc",
"sdccu",
"sdsu webportal",
"sdsu",
"sdsu blackboard",
"sdcc",
"sd card",
"sdn",
"sdro"
]
]
Obtained from google suggest with this URL:
http://suggestqueries.google.com/complete/search?output=firefox&hl=en&q=sd
I have tried deserializing it like this:
dynamic objson = JsonConvert.DeserializeObject(res);
But it is not useful because I need it into a class object.
And also using types:
public class SuggestClass
{
public string search { get; set; }
public string[] terms { get; set; }
}
var result = JsonConvert.DeserializeObject<SuggestClass>(res);
But it always throw exception.
I do not know how can I do it without having name fields.
EDIT:
Another JSON:
["text",["textura","textos bonitos","texto argumentativo","textos","textos de amor","texto expositivo","texturas minecraft","textos de reflexion","texture pack minecraft","textos en ingles"]]
That's tricky...
But since it's an array, you could create a factory method to parse SuggestClass out of given JArray.
public void SomeMethod()
{
string json =
"[\"sd\",[\"sdg\u0026e\",\"sdlc\",\"sdccu\"" +
",\"sdsu webportal\",\"sdsu\",\"sdsu blackboard\","+
"\"sdcc\",\"sd card\",\"sdn\",\"sdro\"]]";
var factory = new Factory();
var suggest = factory.Create(json);
Console.WriteLine(suggest);
}
public class Factory
{
public SuggestClass Create(string json)
{
var array = JArray.Parse(json);
string search = array[0].ToString();
string[] terms = array[1].ToArray().Select(item => item.ToString()).ToArray();
return new SuggestClass {Search = search, Terms = terms};
}
}
public class SuggestClass
{
public string Search { get; set; }
public IEnumerable<string> Terms { get; set; }
public override string ToString()
{
return string.Format("Search={0},Terms=[{1}]",
Search, string.Join(",", Terms));
}
}
Would print to console:
Search=sd,Terms=[sdg&e,sdlc,sdccu,sdsu webportal,sdsu,sdsu blackboard,sdcc,sd card,sdn,sdro]
And the other JSON you provided:
Search=sd,Terms=[sdg&e,sdlc,sdccu,sdsu webportal,sdsu,sdsu blackboard,sdcc,sd card,sdn,sdro]
Search=text,Terms=[textura,textos bonitos,texto argumentativo,textos,textos de amor,texto expositivo,texturas minecraft,textos de reflexion,texture pack minecraft,textos en ingles]
Just used the JSON visualizer in visual studio. This is how it looks like.
It is an array of multiple types. The following code can be used to parse it. But it is not perfect yet.
var objson = JsonConvert.DeserializeObject<object[]>(res);
So I think #Mikko answer has a better approach..

A generic async method for requesting optionally paginated lists of different resources

I am accessing a REST API which returns a list of resources in JSON format:
{
"products": [
{ ... },
{ ... }
]
}
When the list is big (>50 items) the response becomes paginated and an additional pagination item is added to the root node of returned JSON like that:
{
"pagination": {
"results" : 490,
"page" : 1,
"page_size" : 50,
"pages" : 10
},
"products": [
{ ... },
{ ... }
]
}
In order to cater for that I have a PaginatedList class (probably not the best name) which looks like that:
public class PaginatedList
{
[JsonProperty("pagination")]
public Pagination Pagination { get; set; }
}
a ProductList class that looks like that:
public class ProductList : PaginatedList
{
[JsonProperty("products")]
public List<Product> Products { get; set; }
}
a Pagination class like that:
public class Pagination
{
[JsonProperty("results")]
public int Results { get; set; }
[JsonProperty("page")]
public int Page { get; set; }
[JsonProperty("page_size")]
public int PageSize { get; set; }
[JsonProperty("pages")]
public int Pages { get; set; }
}
To retrieve my resource I use:
public List<Product> GetProducts()
{
return getResourceAsync<ProductList>(productsResourceName).Result.Products;
}
and:
async Task<T> getResourceListAsync<T>(string resourceName)
{
var url = string.Concat(BaseUrl, resourceName);
var credentials = new NetworkCredential(Username, Password);
var handler = new HttpClientHandler { Credentials = credentials };
using (var client = new HttpClient(handler)) {
var response = await client.GetAsync(url);
var contentString = await response.Content.ReadAsStringAsync();
var resource = await JsonConvert.DeserializeObjectAsync<T>(contentString);
return resource;
}
}
Adding support for pagination inside the GetProducts method would be pretty easy but that would mean duplicating very similar code for every type of resource (products, customers, suppliers, etc). The question is, how do I get the getResourceListAsync method so that it supports paginated and non-paginated lists AND works for different resources?
To make this scenario possible :
// supports both scenarios
var json = #"{ 'products': [ { Id : 1 , Name : 'A' }, { Id : 2 , Name : 'B' } ] }";
var results = Helper.ParseFromJsonResult<Product>(json);
var anotherJson = #"{ 'pagination': {
'results' : 490,
'page' : 1,
'page_size' : 50,
'pages' : 10
},
'products': [
{ Id : 1 , Name : 'A' }, { Id : 2 , Name : 'B' }
]}";
var anotherResults = Helper.ParseFromJsonResult<Product>(anotherJson);
You can use this codes :
public static class Helper
{
private static readonly PluralizationService _NameService =
PluralizationService.CreateService(new CultureInfo("en-us"));
// Provides Plural names [ products for Product ]
// to determinate the name of the result for example products for Product class
private static ModuleBuilder _ModuleBuilder;
static Helper()
{
var asmName = new AssemblyName();
asmName.Name = "MyHelpers";
AssemblyBuilder asmBuilder = Thread.GetDomain().DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
_ModuleBuilder = asmBuilder.DefineDynamicModule("MyHelpers");
// Assembly to put runtime generated classes to that.
}
private static readonly IDictionary<Type, Type> _HelpersCache = new Dictionary<Type, Type>();
public static List<T> ParseFromJsonResult<T>(String json)
{
Type resultType = null;
var entityType = typeof(T);
var pluralName = _NameService.Pluralize(entityType.Name).ToLowerInvariant();
// products for Product class
if (_HelpersCache.ContainsKey(entityType))
{
// better performance
resultType = _HelpersCache[entityType];
}
else
{
// need another runtime generated class
// result :
/* public class products
{
public List<Product> products;
}
*/
TypeBuilder resultTypeBuilder = _ModuleBuilder.DefineType(pluralName, TypeAttributes.Public);
FieldBuilder field = resultTypeBuilder.DefineField(pluralName, typeof(List<T>), FieldAttributes.Public);
resultType = resultTypeBuilder.CreateType();
_HelpersCache.Add(entityType, resultType);
}
Object result = JsonConvert.DeserializeObject(json, resultType);
return (List<T>)resultType.GetField(pluralName).GetValue(result); // get products field value
}
}
I hope this helps, Let me know if you want more information
Good luck
I see your problem. Was stuck in the same one actually. What I ended up doing was an interface that my classes i want to paginte implement. As I did this for export of data it is called IExportable. This thing returns a PagedResult and takes a IPagedRequest.
So the point is that my code in the task is just working with interface and does not actually know what the end type is. PagedResult is a type with generics.
Hope it helps, trying to write this from top of my head without the source.

Json serialization in asp.net mvc c#

public ActionResult About()
{
List listStores = new List();
listStores = this.GetResults(“param”);
return Json(listStores, “Stores”, JsonRequestBehavior.AllowGet);
}
Using the above code I am able to get the below result :
[{"id":"1","name":"Store1","cust_name":"custname1","telephone":"1233455555",
"email":"abc#ac.com","geo":{"latitude":"12.9876","longitude":"122.376237"}},
{"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454",
"email":"nfnf#ac.com","geo":{"latitude":"12.9876","longitude":"122.376237"}},
how would I able to get the result in below format? Would need stores at the beginning of the result.
{
"stores" : [
{"id":"1","name":"Store1","cust_name":"custname1","telephone":"1233455555",
"email":"abc#ac.com",
"geo":{"latitude":"12.9876","longitude":"122.376237"}},
{"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454",
"email":"nfnf#ac.com","geo":{"latitude":"12.9876","longitude":"122.376237"
}} ] }
Try
return Json(new { stores = listStores }, JsonRequestBehavior.AllowGet);
In the above statement, you're creating a new object with a property named "stores", which is then populated with the array of items from the list.
You could also use a class, something defined like so:
[DataContract]
public class StoreListJsonDTO
{
[DataMember(Name = "stores")]
public List Stores { get; set; }
public StoreListJsonDTO(List storeList)
{
this.Stores = storeList;
}
}
Then in your code, you'd do:
var result = new StoreListJsonDTO(listStores);
return Json(result, JsonRequestBehavior.AllowGet);

Categories