I am used to making JavaScript objects like below but what would be the fastest way of doing the same with C#? It is just static data that will be serialized for a post that will not change. In the past with C# I would create classes and lists for everything but in this scenario I just need to send some practice data. Any help would be appreciated
var data = {
"dealer": {
"keyId": "vfase32sd",
"name": "mark"
},
"seller": [
{
"email": "email#email.com",
"active":false
}
],
"state": [
"TN",
"TX"
]};
See Object and Collection Initializers
Object initializer with Anonymous types
Although object initializers can be used in any context, they are especially useful in
LINQ query expressions. Query expressions make frequent use of
anonymous types, which can only be initialized by using an object
initializer, as shown in the following declaration.
var pet = new { Age = 10, Name = "Fluffy" };
For example:
var data = new
{
dealer = new
{
keyId = "vfase32sd",
name = "mark"
},
seller = new[] {
new {
email= "email#email.com",
active= false
}
},
state = new[]{
"TN",
"TX"
}
};
The rule for converting that js object to c# object is simple:
"x": will be x=
{} will be new {}
[] will be new []{}
Values remain untouched
You can use dynamic typing feature of C#
var data = new
{
dealer = new
{
keyId = "vfase32sd",
name = "mark",
},
seller = new[]
{
new
{
email = "email#email.com",
active = false
}
},
state = new []
{
"TN",
"TX"
}
};
Related
I have the following JavaScript object which prints the following JSON using
var str = JSON.stringify(finalNodesData, null, 2);
console.log(str);
Printed JSON
[
{
"jobDate": "2023-01-03 13:48:29.402",
"id": "b186c313-a2f3-44a8-9803-066c6d52e8a0"
},
{
"jobDate": "2023-01-03 13:57:19.988",
"id": "db182f5e-9622-42e9-bbe8-19bee4d878d4"
}
]
How can I add two new elements "submitedBy" and "submitReason" to the JSON? I want my JSON to look like
{
"submitedBy": "Bob Smith",
"submitReason": "Because of an error",
"nodeData": [
{
"jobDate": "2023-01-03 13:48:29.402",
"id": "b186c313-a2f3-44a8-9803-066c6d52e8a0"
},
{
"jobDate": "2023-01-03 13:57:19.988",
"id": "db182f5e-9622-42e9-bbe8-19bee4d878d4"
}
]
}
I want to use JavaScript variables to generate the JSON, and not concatenate strings.
Note: Your new JSON is no longer an Array, but an Object.
finalNodesData = {};
finalNodesData["nodeData"] = nodeData; // this is your old data
finalNodesData["submitedBy"] = "Bob Smith";
finalNodesData["submitReason"] = "Because of an error";
var str = JSON.stringify(finalNodesData, null, 2);
console.log(str);
The best way of doing this is adding the data to the JavaScript object before converting it to JSON.
let finalNodesData = [
{
jobDate: "2023-01-03 13:48:29.402",
id: "b186c313-a2f3-44a8-9803-066c6d52e8a0"
},
{
jobDate: "2023-01-03 13:57:19.988",
id: "db182f5e-9622-42e9-bbe8-19bee4d878d4"
}
]
const submittedBy = "Bob Smith"
const submitReason = "Because of an error"
finalNodesData = {
submittedBy,
submitReason,
nodeData: finalNodesData
}
var str = JSON.stringify(finalNodesData, null, 2);
console.log(str);
I'm trying to create a function that when called will update a specific object in json file. However, it updates the object as well as creating a new one.
I've tried many different methods in trying to get this to work, but all have failed. The closest I've got to it working is the code shown below, but it still doesn't do what is required.
This is my function:
var fs = require('fs');
var _ = require("underscore");
module.exports = {
personalUpdate: function (id, forename, surname, dob, gender, callback) {
let rawdata = fs.readFileSync('data.json');
let data = JSON.parse(rawdata);
let filtered = _.where(data['students'], { id: id });
let all = filtered[0];
all.forename = forename;
all.surname = surname;
all.dob = dob;
all.gender = gender;
data["students"].push(all);
fs.writeFileSync('data.json', JSON.stringify(data, null, 2), (err) => {
if (err) throw err;
});
callback("success");
}
}
And this is the JSON file that I want to update:
{
"teachers": [
{
"name": "",
"email": "",
"password": "",
"formGroup": "",
"id": ""
}
],
"students": [
{
"surname": "test",
"forename": "test",
"dob": "",
"homeAddress": "",
"homePhone": "",
"gender": "",
"tutorGroup": "",
"schoolEmail": "",
"grades": [
{
"french": 8,
"maths": 7
}
],
"id": ""
},
{
"surname": "test2",
"forename": "test2",
"dob": "",
"homeAddress": "test2",
"homePhone": "",
"gender": "",
"tutorGroup": "",
"schoolEmail": "",
"grades": [
{
"french": 9,
"maths": 8
}
],
"id": ""
}
]
}
I had to remove and change the objects and info inside them, as it contained confidential information.
When running this function, it finds the object that is specified in the parameter. It then updates that object, but it then creates another object at the bottom of the original JSON object, which it is not supposed to.
Also, is there a better way to update the specific objects in the JSON file?
tl;dr
The result set is duplicating because you are pushing it into the array
The change is being applied due to the variables holding the same object reference, so they are being mirrored across objects that share the same pointer.
Explanation
It creates a new one due to the data["students"].push(all); instruction.
When you manipulate objects in javascript you need to be aware of how the reference between them work, so you can avoid bugs and use them in your benefit.
For example, take this set of instructions:
let a = {"x": 1};
let b = a;
b.x = 3;
console.log(a) // it will output {"x": 3}
Notice that we:
Create an object with the prop x equal 1 and assign it to the variable a
Initialize a variable b with the value of a
Change the property x on the variable/object b
Then we can observe that the change was also reflected in the variable a, due to the object reference.
So, basically this is exactly what is happening with your instructions when you do all.forename = forename; it changes the variable all, but also the original object which it derives from.
Here is a nice reference that explains this concept more in-depth
#EDIT
I strongly advise you not using the sync version of functions like readFileSync since this blocks the event loop. Here is the official guidelines about it
I have an array in Knockout view model which looks like so:
this.Activities = ko.observableArray([
{ "date": "28/11/2012 00:00:00",
"activities": [
{ "company": "BOW",
"description": "Backup Checks",
"length": "60"
},
{ "company": "AMS",
"description": "Data Request",
"length": "135"
},
]},
{ "date": "30/11/2012 00:00:00",
"activities": [
{ "company": "BOW",
"description": "Backup Checks",
"length": "60"
},
{ "company": "SLGT",
"description": "Software Development",
"length": "240"
},
{ "company": "BOW",
"description": "Data Request",
"length": "30"
},
]},
]);
I use this code to add a new element to it:
this.Activities.push(new NewActivity(company, description, length, fullDate));
Which uses NewActivity function:
function NewActivity(company, description, length, date) {
this.date = date;
this.activities = [{ "company": company, "description": description, "length": length }];
}
And it works fine. However, it creates an entirely new object every time it is getting released. I need to implement a condition when the code would check for the date of the objects already created. If the newly created object had the same date, activity details should be added to activities array within the Activities array for that date.
How can I do it?
All of the data for the Activities array comes from the model in the strongly typed view in MVC application:
this.Activities = ko.observableArray([
#foreach (ActivitySet aSet in Model)
{
#:{ "date": "#aSet.Date",
#:"activities": [
foreach(Activity a in aSet.Activities)
{
#:{ "company": "#a.Companies.Select(c => c.Title).Single()",
#:"description": "#a.Descriptions.Select(c => c.Title).Single()",
#:"length": "#a.LengthInMinutes"
#:},
}
#:]},
}
]);
I suggest that you create a few entities describing your activities:
// Details
function ActivityDetails(company, description, length) {
this.company = ko.observable(company);
this.description = ko.observable(description);
this.length = ko.observable(length);
}
// Activity
function Activity(date, activityDetails) {
this.date = ko.observable(date);
this.details = ko.observableArray(activityDetails);
}
The you can control activities in the following manner:
function ViewModel () {
var self = this;
this.Activities = ko.observableArray([
// Activities here
]);
this.addActivity = function (activity) {
var flag = false;
ko.utils.arrayMap(self.Activities(), function (item) {
// Flag is here so it doesn't keep checking further in iterations
if (!flag) {
if (item.date() === activity.date()) {
item.details.push(activity.details);
flag = true;
}
}
});
// Case where activity date was not found in existing records
if (!flag) {
self.Activities.push(activity);
}
}
}
This requires your view model to have a custom add method which I have provided an example of. Note that everywhere I am resolving observable values, so if you are using non-observable ones remove the function calls.
I have a JSON structure;
{
books: [
{"id": "book1", "title": "Book One"},
{"id": "book2", "title": "Book Two"}
]
}
Which represents;
function BookList() {
this.books = new Array();
}
BookList.prototype.addBook = function(b) {
this.books[this.books.length] = b;
}
function Book(id, title) {
this.id = id || '';
this.title = title || '';
this.chapters = new Array();
}
Book.prototype.addChapter = function(c) {
this.chapters[this.chapters.length] = c;
}
What is the best way to create the array of Book objects within the BookList?
With jQuery you can just use $.parseJSON(jsonData).books to assign the BookList array and have the properties values automatically but that doesn't create the required objects with the prototype functions.
Is the only way to iterate over the JSON array and create each Book object individually? This question covers creating a particular prototyped object from JSON: Parse JSON String into a Particular Object Prototype in JavaScript.
I supposed this could be extended to apply to the BookList with the right level of iteration and from there applied to the chapters for the Book object.
Is it possible to change an object type after initial assignment with $.parseJSON?
var json = {
books: [
{"id": "book1", "title": "Book One"},
{"id": "book2", "title": "Book Two"}
]
};
var list = new BookList();
list.books = json.books.map(convertToBook);
function convertToBook(book) {
return new Book(book.id, book.title);
}
Say I want the object to be something like this:
var Book = {
title: "the catcher in the rye",
price: "80.98",
characters: [{name: "holden caulfield", age: 16, height:"6.2"},
{name: "phoebe caulfield",age:13, height: "5"}]
};
EDITED
question:
characters array is built by adding a character one by one. How can do this while making sure that name, age and height properties are defined as above.
Something to the effect of?
Book.characters.add({name: "phoebe caulfield",age:13, height: "5"});
I would like to be able to define this programmatically, ie add properties to the object rather than define it like this.
Is this possible?
You can do it in dynamic code (rather than a static declaration) like this:
var Book = {};
Book.title = "the catcher in the rye";
Book.price = "80.98";
Book.characters = [];
Book.characters.push({name: "holden caulfield", age: 16, height: "6.2"});
Book.characters.push({name: "phoebe caulfield", age: 13, height: "5"});
Do you mean like this?
var Book = {}; // empty object
Book.title = "the catcher in the rye";
Book.price = 80.98;
Book.characters = [];
var character = {
"name": "holden caulfield",
"age": 16,
"height": 6.2
};
Book.characters.push(character);
var Book={};
Book.title="the catcher in the rye";
Book.price="80.98";
var characters=[];
characters.push({name: "holden caulfield", age: 16, height:"6.2"});
characters.push({name: "phoebe caulfield",age:13, height: "5"});
Book.characters=characters;
...etc.
It certainly is! Javascript is a completely dynamic language - if you want a new property on an object, just set it!
e.g.
var myObject = {}; // empty object
myObject.myProperty = 5; // creates the new property and sets it to 5.
myObject.nestedObject = { prop1: 6, 'long-property-name': 'lolcats' };
I think you're looking for a JSON stringifier:
http://www.json.org/js.html
This will allow you to create your object:
var myobj = { };
myobj.myprop = "value";
alert(JSON.stringify(myobj));
using the map function is the easiest way I've found to build an array of objects. The snippet below, constructs the objet you want in just 2 statements:
var Book = {
title: "the catcher in the rye",
price: 80.98
};
Book.characters = [6.2, 5].map(el => {
return {
name: 'caulfield', age: 14, height: el
};
});
// Output
JSON.stringify(Book);
{
"title": "the catcher in the rye",
"price": 80.98,
"characters": [
{ "name":"caulfield", "age":14, "height":6.2 },
{ "name":"caulfield", "age":14, "height":5 }
]
}