C# Serialize to JS Object without quoting some values [duplicate] - javascript

This question already has answers here:
How to serialize a raw json field?
(2 answers)
Closed 5 years ago.
I am creating a javascript object from a c# object and one of the properties is a reference to a js function , but when serializing the object the value has quotes around it witch makes it a normal string and not a function.
this is the current output :
{ "x": "functionNameToBeCalled" }
But I need it to be like
{ "x": functionNameToBeCalled }
Is there anyway to do this with Json.Net or do I have to create the js object manually?
I tried using the JsonPropertyAttribute but can't figure out which property to set!!!

change the way of calling your method, something like this:
window.z= function(){ console.log('hi');}
var b = { a: 'z'}
window[b.a]();
so no need to change json serialization behavior.

Related

Undefined as value from Json with Javascript [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
{
pet: {
"0.628": 92694.5,
"8739.836": 96391.94
},
try: {
//same
}
}
When I specify the key, I get the values but i am trying to read all the values without knowing the keys. I have even tried regex, but nothing seems to be working. As you can see i am fairly new. So sorry if this was a stupid question.
console.log(data.pet) // Gives [Object Object]
console.log(data.pet["0.628"])//Gives the value
console.log(data.pet[0])//Gives undefined
I don't see in what context you'd want to access a json object without knowing the keys.
but what you can do is to parse the json file into a javascript object, and call Object.keys() to get the keys of that object

getJSON replace object with variable [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I have json file called list.json:
{
"nvg":{
"Title":"title",
"Description":"description",
"Image":"",
"URL":{
"DEV":"https://dev.com",
"TEST":"https://test.com",
"PROD":"https://prod.com"
}
}
I have an array
var apps = ["nvg"];
I want to access the json data and extract the object based on the variable's value. Does anyone know how to pass the [i] into the json call?
var getConfig = $.getJSON( data, function( json ) {
var apps = ["nvg"];
apps.forEach(function(i){
alert(i);
alert(json.i.URL.DEV);
});
});
Like the code above, but to actually the "i" to pass the value defined in the array?
You should be able to access the data using your Javascript object like an object or array. This means you can do the following:
alert(json[i].URL.DEV);

How to write Extensible methods in JavaScript [duplicate]

This question already has answers here:
Add method to string class
(6 answers)
How do I write an extension method in JavaScript?
(2 answers)
Closed 5 years ago.
I need to write a method I will call on a string literal in JavaScript. A method that I want to call:
"Javascript".toKampala();
Does that feature exist in JavaScript? and if it does How do I write such a method (toKampala()) on a JavaScript literal or any object?
In Kotlin I did it like this;
fun String.toHenry():String{
return "$this Henry";
}
and I can call
"chalres".toHenry()
Every string is default has a prototype, which is the String.prototype object and it can access anything which are defined there.
You need add that method in the String.prototype and it will be accessible from any string. You can access the current string in that function by this.
String.prototype.toHenry = function() {
return this + ' Hentry';
};
console.log('charles'.toHenry());

how to add key and a value to a json with same existing Key using Javascript [duplicate]

This question already has answers here:
How to add new property with same key name inside declared object?
(3 answers)
Closed 6 years ago.
I have a json variable like this
var jsondata={"key1":"val1", "key2":"val2"}
I want to push another object with same existing key, and i want that my variable will be like this
var jsondata={"key1":"val1", "key2":"val2", "key1":"val3"}
I tried jsondata["key1"] = "val3", but it didn't return the wanted result
Thank you in advance.
you cannot, as it is a map.
but you could create this json :
var jsondata={"Name":["Jhon","James"], "Age":40}
You can't use the same key in an object. Your question suggests that the logic behind your data structure is wrong.
An alternative:
Use a different field name, i've used "_Name" below, but perhaps "Second_Name" would be more appropriate. Unsure what your json data is modelling.
var jsondata={"Name":"Jhon", "Age":40, "_Name":"James"};
Or perhaps it makes sense to store an array of people, is that what you're trying to achieve? i.e. you have two people, with the names "Jhon" and "James"?
var jsondata={
"people": [
{"Name":"Jhon", "Age":40},
{"Name":"James"}
]
};

How to convert a String that looks like JSON or a JS object, to an actual JS object? [duplicate]

This question already has answers here:
How to parse JSON easily?
(3 answers)
Closed 6 years ago.
The String I am talking about was initially a part of a JS object like:
var nameVal = "Jacob";
var favNumbersVal = "{\"firstNum\":0, \"secondNum\":1, \"thirdNum\":2}";
var aJSObject = {
"name" = nameVal,
"favNumbers" = favNumbersVal
};
The variable I am interested in is favNumbersVal. Please notice that the starting and ending " around the value of favNumbersVal are the normal double quotes we put around a String whenever we define a String.
The format of the value of favNumbersVal is coming from a library dynamically.
The question is that how do I convert the value of favNumbersVal to a JS object, so that when I later convert sJSObject to JSON using JSON.stringify(), the value of aJSObject becomes a JSON object, and the value of favNumbers becomes a JSON object nested inside the aforementioned JSON object.
Using JSON.parse():
var favNumbersVal = "{\"firstNum\":0, \"secondNum\":1, \"thirdNum\":2}";
console.log(JSON.parse(favNumbersVal));

Categories