|
I can't help but think that a quick and dirty way to accomplish this would be to use a dynamic type to call JsonConvert.DeserializeObject (if you're using JSON.NET), and then use a little bit of reflection to pick out the public properties from here.
[Edit]
Thinking about this a bit further, it should be possible to use the JavaScriptSerializer like this:
Dictionary<string, object> values = new JavaScriptSerializer().DeserializeObject<string, object>(myJson);
This space for rent
modified 14-Sep-16 3:25am.
|
|
|
|
|
The in-build JSON functionality isn't great at these kinds of things. If you use a third party parser like Newtonsoft[^] it supports dynamic JSON better as it will build a collection of objects from the JSON that you can then traverse.
|
|
|
|
|
If you're using Newtonsoft Json.net, then you can get all the properties from the JSON objects something like the following:
JObject results = JObject.Parse(jsonString);
foreach (KeyValuePair<string, JToken> token in results) {
Console.WriteLine("object: token.Key");
foreach (JProperty prop in token.Value) {
Console.WriteLine("\t {0}:{1}", prop.Name, prop.Value);
}
}
|
|
|
|
|
Hi, below is my code snippet which uses the code you've provided. Although I'm not getting any errors, I'm also not getting anything at all.
string testString = string.Empty;
string jsonString = "{\"ID\": \"xx\", \"font-size\": \" 2em\",\"color\": \"blue\"}";
JObject result = JObject.Parse(jsonString);
foreach (KeyValuePair<string, JToken> token in result)
{
foreach (JProperty prop in token.Value)
{
testString = prop.Name + " : " + prop.Value;
}
}
modified 14-Sep-16 11:20am.
|
|
|
|
|
The reason for that is that you don't have a collective object. Normally, the JSON string would look something like:
string jsonString = "{theObject:{\"ID\": \"xx\", \"font-size\": \" 2em\",\"color\": \"blue\"}}";
Without the containing object, you will find your properties in the Key/Value pairs of the outer loop.
The way I got to all of this kind of stuff (I'm reading JSON from tmdb.org) was setting up this kind of test in a small console app, and using the debugger to step through and look at each of the JObject, JToken, and JProperty objects as it went through. I managed to get how it all holds together pretty quickly that way.
Cheers, and good luck.
Mick
|
|
|
|
|
Alternatively, if you don't have the containing object, parse the string directly into a JToken rather than the JObject.
Code under those circumstances would be:
tring testString = string.Empty;
string jsonString = "{\"ID\": \"xx\", \"font-size\": \" 2em\",\"color\": \"blue\"}";
JToken result = JObject.Parse(jsonString);
foreach (JProperty prop in result)
{
testString = prop.Name + " : " + prop.Value;
}
|
|
|
|
|
Thank you, it works like a charm.
|
|
|
|
|
i am working with EF code first. so initially i have no tables in database. so i wrote some class and when query those class then i saw EF code first create those tables in db but when i create sql server view in db and later map that view with my code in c# & EF project and when i try to query that view then i was getting error message.
Additional information: The model backing the 'TestDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database
i understand that EF is telling me to do the migration but if i migrate then EF will create that view in db again when the view is in db already exist.
so tell me how could i inform EF that my view is already is in db so migration is not required.
please guide me. thanks
tbhattacharjee
|
|
|
|
|
EF Code first does not support views, and you cannot use views as entities. If you tried to do that, it would explain the message. It's designed so that if you want a view you can define a view model and load it with an IQueryable in order to handle projections, rather than DB side code objects.
If you want to map views, you'll need to should do Database First so that it can lean on an .edmx mapping.
Edit: Corrected content based on Mr Deeming's answer.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
modified 13-Sep-16 8:47am.
|
|
|
|
|
Nathan Minier wrote: EF Code first does not support views, and you cannot use views as entities.
Yes it does, and yes you can.
You can't create a view through automatic migrations, but a manual migration with the CREATE / DROP VIEW commands will work fine.
Configure the mapping from the entity to the view, and EF won't know the difference.
The only problem you might encounter is if your view isn't updateable, and you need to add, update or remove entities from it. In that case, you'd need to use MapToStoredProcedures [^] to map the insert, updated and delete commands to stored procedures.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
would you please like to come with code what u said. just post a sample code for handing view in EF code first.
u said "you can define a view model and load it with an IQueryable in order to handle projections, rather than DB side code objects." please show this with sample code. thanks
tbhattacharjee
|
|
|
|
|
Well, Mr Deeming's answer might suit you better, but for a quick and dirty example:
public class User
{
public string UserName {get;set;}
public string PasswordHash {get;set;}
public DateTime Created { get; set; }
public int RoleId { get; set}
[Foreginkey("Role")
public virtual Role Role { get; set;}
}
public class Role
{
public int Id { get; set; }
public string Title { get; set; }
public int GroupId {get; set; }
[ForeignKey("GroupId")]
public virtual Group AssignedGroup {get; set;}
public virtual ICollection<User> Users { get; set; }
}
public class UserViewModel
{
public string UserName {get;set;}
public string Role {get; set;}
public DateTime Created { get; set; }
public UserViewModel(){}
public UserViewModel(User user)
{
UserName = user.UserName;
Role = user.Role.Title;
Created = user.Created;
}
}
public class UserController: ApiController
{
...
public IEnumerable<UserViewModel> GetUsers()
{
return myDbContext.Users.Select(x => new UserViewModel(x)).ToArray();
}
}
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
what kind of question i asked and what u answer.
tbhattacharjee
|
|
|
|
|
You'll need to create the view via a manual migration:
public partial class CreateYourViewMigration : DbMigration
{
public override void Up()
{
const string script = @"CREATE VIEW dbo.YourView As ...";
var context = new YourContext();
context.Database.ExecuteSqlCommand(script);
}
public override void Down()
{
const string script = @"DROP VIEW dbo.YourView;";
var context = new YourContext();
context.Database.ExecuteSqlCommand(script);
}
}
Entity Framework Code First Migrations[^]
If your view is not updateable, and you need to add/edit/delete the entities, then you'll need to use MapToStoredProcedures :
Entity Framework Code First Insert/Update/Delete Stored Procedures (EF6 onwards)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
What is the actual use of resource file in c# or in WCF services ?
|
|
|
|
|
There is a good description of .RESX files here: Resources in .Resx File Format[^] which explains the file format (XML) as well as touching on what it holds.
Basically, it's a human-and-machine readable file which holds the images, icons, text (including - but not restricted to - localisation strings), sounds, and other resources that your app needs to have compiled into it that the compiler uses to build your final EXE or DLL assembly.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I have a requirement to prevent the user from adding, renaming, or deleting folders and files. I've never done Windows hooks before.
Is this possible? Can someone point me n the right direction?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
There are no hooks per se for NTFS. You'd be writing a filter driver for NTFS.
Why couldn't you do this with standard NTFS permissions?
|
|
|
|
|
I didn't mention NTFS. I don't really have any preference as to how it's done.
How do I do this with file permissions? Maybe this[^]?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
What are you doing and why are you hell bent on doing this in code?
|
|
|
|
|
It's a requirement for an app I'm working on
If it's not broken, fix it until it is
|
|
|
|
|
That doesn't answer my questions at all. What's the point of doing this at all?
|
|
|
|
|
To prevent the user from doing anything to app controlled folders and files.
If it's not broken, fix it until it is
|
|
|
|
|
If you're talking about under the Program Files folder you don't have to do anything. Everything under Program Files is ReadOnly to normal users.
But, what already exists depends on where you're these files/folder you're talking about are in the directory tree.
|
|
|
|
|
I have an app that has folders created and files copied to it without the user's interaction. Then permissions are applied.
If the user does not have rights, then they cannot add files, rename them, delete them, or likewise manipulate folders.
If it's not broken, fix it until it is
|
|
|
|