Hi, I had faced one issue like this. I have an JSON object but I don't know any key name but I need to get the all the key and corresponding value from JSON object using client side JavaScript. Suddenly I wondered whether it's possible or not, after that I had done lot of workaround and finally got this solution. See the below example.
function getKeyValueFromJSON() {
In this example I have created the one json array as string, and converted this string into JSON object using eval() function. Using for-each loop I got all the key value from jsonObj, and finally using that key I got the corresponding value.
Finally I got the alert like this,
Key: a value:10
Key: b value:20
Key: c value:30
Key: d value:50
During this workaround I got one more idea, using this same way I got all the variable name and corresponding value from JavaScript object. See the below example.
This is my JavaScript class. Here I have declared a function for variable d.
function testJavaScriptClass() {
Now I am going to create one object for this class and going to get the variable name and corresponding value from JavaScript object.
function getKeyValueFromJSObject() {
Here I have checked the key (variable) is whether a function or not. If it is function then, I executed the function and get the result.
Finally I got the alert this.
Key: a value:10
Key: b value:20
Key: c value:30
Key: d value: "Test result from inside this function"
Hope this help...!
function getKeyValueFromJSON() {
var jsonObj = {a:10,b:20,c:30,d:50};
for(var key in jsonObj) {
alert("Key:
" + key + " value: " + jsonObj[key]);
}
}
Finally I got the alert like this,
Key: a value:10
Key: b value:20
Key: c value:30
Key: d value:50
During this workaround I got one more idea, using this same way I got all the variable name and corresponding value from JavaScript object. See the below example.
This is my JavaScript class. Here I have declared a function for variable d.
function testJavaScriptClass() {
this.a = 10;
this.b = 20;
this.c = 30;
this.d= function() {
return "Test
result from inside this function";
};
}
function getKeyValueFromJSObject() {
var testObj = new
testJavaScriptClass();
for(var key in testObj) {
if(typeof testObj[key] !=
"function") {
alert("Key: " + key + " value: " + testObj[key]);
}
else {
var value = testObj[key]();
alert("Key: " + key + " value:
" + value);
}
}
}
Here I have checked the key (variable) is whether a function or not. If it is function then, I executed the function and get the result.
Finally I got the alert this.
Key: a value:10
Key: b value:20
Key: c value:30
Key: d value: "Test result from inside this function"
Hope this help...!
Comments
to find the field name of the second record
{
"cmd":"action",
"action":"find",
"params":{"strategy":"id",
"selector":"com.ebay.mobile:id/button_sign_in",
"context":"",
"multiple":false}
}
Try looping thru this Object
var myObj = {id:1, name="joe", age=31", isAGenius=true}
JSON Response:
"CompleteResult":{
"Result1": {
"abc": 1,
"def": 2
},
"Result2": {
"abc": 3,
"def": 4
}
}
Note:
=======
I need to know the following
1) length of CompleteResult
2) how to get the value "Result1" i.e, I may have Result3 in next response, I dont know what value will be coming there. So I have to get the value dynamically
Sanjoy
{
'collectionName' : 'fouser',
mapReduce : {
map : 'function () {
var docid = this._id.valueOf();
var date = this.date;
var doc = this,
rec = doc.records;
if( docid == ObjectId("533a91b2263e03670b000009") ){
var i = 0;
Object.keys(rec).forEach(function(key) {
emit (NumberInt(i), { recordid : ObjectId(key) , completed : rec[key].completed, datedue:rec[key].due});
i = i+1;
});
}
}',
reduce : 'function (key, values) {
var allDetails = [];
var user = { recordid: null, completed : null};
for( var index = 0; index < values.length; index++){
user.recordid = values[index].recordid;
user.completed = values[index].completed;
allDetails.push(user);
user = { recordid : null, completed : null};
}
return {list : allDetails}
}',
out : 'list',
},
sort:{value.completed:-1}
}
Instand of hot coding i passing dyanamically means showing error like "object.keys called on non object in object.keys Object.keys(rec).forEach(key) ".How can i resolve please help me .I'm using this inside jaspersoft
Selenium Training in Chennai
Selenium Training in Bangalore
Selenium Training in Chennai
Selenium Training in Bangalore
reviewcouncil
Technology
Ethical Hacking Course in Chennai
Hacking Course in Chennai
Learn Ethical Hacking
Ethical Hacking Training Institute in Chennai
Ethical Hacking Course in Anna Nagar
Node JS Training in Chennai
Node JS Course in Chennai
Photoshop Classes in Chennai
Photoshop Course in Chennai
Android Training in Anna Nagar
android training in velachery
Android Training in Tambaram
Android Training in OMR
Android Training in T Nagar
Android Training in Adyar
Android training in vadapalani
Android Training in Porur
android training in nungambakkam
python training in chennai
python online training in chennai
python training in bangalore
python training in hyderabad
python online training
python flask training
python flask online training
python training in coimbatore
java training in chennai
java training in annanagar
aws training in chennai
aws training in annanagar
python training in chennai
python training in annanagar
selenium training in chennai
selenium training in annanagar
data analytics in hyderabad
Best Data Science Training Institute in Pune
best data science training institute in bangalore
Best RPA Training in Pune
AWS Training in Pune
RPA course in Chennai
Android Course in Chennai
Angularjs course in Chennai
.net coaching centre in chennai
Hacking Course in Chennai
Digital Marketing Training in Chennai
Top Tips to become a successful swift programmer
How become a successful swift programmer
PhoneGap Performance
PhoneGap Architecture
Azure Data Factory course in Ameerpet,Azure Data Engineer Training Online Hyderabad,azure training in hyderabad ameerpet,Azure Data Factory online Training in Hyde
Best Digital Marketing Institutes in India
Digital Marketing Institutes in chandigarh
Digital Marketing Courses In hobart
purpose of context:component-scan tag. This tag automatically final year projects for computer science
scans all the controller class presents in base-package and registered into context. In other words, it’s just looks what are all the classes Final Year Project Centers in Chennai
have @Controller annotation and map the controller into corresponding view. This information is maintained in context of the application.