I'm newbie to Angular Js.I would like to Create Complex Json Object for my Xml settings Like
string Jsonobject=" {'Email': { 'UserName': 'a','Password': 'a'}}";
So far I tried
$scope.save = function () {
var AddSettings = $resource('../AddSettings/');--Calling my WCF REST SERVICE
var adminSettings = new AddSettings();
adminSettings.Title = $scope.inputtitle;
adminSettings.HomeUrl = $scope.intputhomeurl;
//var arr = [];
//arr.push(addmodule.Title);
//arr.push(addmodule.HomeUrl);
//addmodule.LogoSettings = arr;
adminSettings.$save();
It's generating following Json Object : { "UserName": "a","Password": "a"},Can any one please guide me to create a json Object like " {'Email': { 'UserName': 'a','Password': 'a'}}" using Angular js.
Thanks in advance
I solved my problem in the following way.
var adminSettings = new AddSettings();
adminSettings.LogoSettings = {
Title: $scope.inputtitle,
HomeUrl: $scope.intputhomeurl,
logo: $("#ImageUpload > img").attr('src')
};
adminSettings.$save();
It's returning Json object as {'Email': { 'UserName': 'a','Password': 'a'}}.
Related
I have multiple json Objects
json1 = [
{'category_id':1,'name':'test1' },
{'category_id':1,'name':'test2' },
{'category_id':1,'name':'test3' },
{'category_id':2,'name':'test2' },
{'category_id':3,'name':'test1' }
];
json2 = [{'category_id':1,'type':'test1'}];
json3 = [
{'category_id':1,'color':'black'},
{'category_id':2,'color':'black'},
{'category_id':3,'color':'white'}
];
I am expecting output like this
final = [
{'category_id':1,'name':'test1','type':'test`','color':'black' },
{'category_id':1,'name':'test2','type':'test`','color':'black' },
{'category_id':1,'name':'test3','type':'test`','color':'black' },
{'category_id':2,'name':'test2','color':'black' },
{'category_id':3,'name':'test1','color':'white' }
];
As i have long json object. Looping is good idea or not ? Does there is any inbuilt function for doing the same.
Using underscore you can achieve it via:
Demo Fiddle
var jsons = [json1, json2, json3];
var final = {};
// merge all data
_.each(jsons, function (jsonArr) {
_.each(jsonArr, function (json) {
final[json.category_id] = _.extend({}, final[json.category_id], json);
});
});
// map it back onto json1
var finalArr = _.map(json1, function (json) {
return final[json.category_id];
});
console.log(finalArr);
Final value of finalArr:
Here is how you can do the same in plain javascript. :)
var final = {};
var jsons = [json1, json2, json3];
for(var i=0;i<jsons.length;i++){
final[i]=jsons[i];
}
Fiddle
EDIT:
Well, you will have to do it programmatically!
I have a json object i want to get data from it!
here is my json object
"[androidVersionName=2.3.3, androidVersionId=10, androidId=fa0bef4b5a48eacb, mobileModel=sdk, mobileManufacturer=unknown, mobileId=GRI34, mobileProduct=sdk, applicationName=com.example.socketclient, applicationVersionName=1.0, applicationVersionCode=1, applicationState=INACTIVE, screenWidth=480, screenHeight=480, screenDensity=240, screenDensityName=hdpi, atdPackages=com.atd.panberes(1)]"
and here is my code :
var json = JSON.parse(data);
var androidVersionName = data.rowsets['androidVersionName'].row;
console.log(androidVersionName);
and i get this error :
Cannot read property 'androidVersionName' of undefined
how can i parse data from this jsonObject?
A valid JSON based on your object:
{
"androidVersionName":"2.3.3",
"androidVersionId":10,
"androidId":"fa0bef4b5a48eacb",
"mobileModel":"sdk",
"mobileManufacturer":"unknown",
"mobileId":"GRI34",
"mobileProduct":"sdk",
"applicationName":"com.example.socketclient",
"applicationVersionName":1.0,
"applicationVersionCode":1,
"applicationState":"INACTIVE",
"screenWidth":480,
"screenHeight":480,
"screenDensity":240,
"screenDensityName":"hdpi",
"atdPackages":"com.atd.panberes(1)"
}
You can't parse it to object. But you can transform it to JS object.
var data = "[androidVersionName=2.3.3, androidVersionId=10, androidId=fa0bef4b5a48eacb, mobileModel=sdk, mobileManufacturer=unknown, mobileId=GRI34, mobileProduct=sdk, applicationName=com.example.socketclient, applicationVersionName=1.0, applicationVersionCode=1, applicationState=INACTIVE, screenWidth=480, screenHeight=480, screenDensity=240, screenDensityName=hdpi, atdPackages=com.atd.panberes(1)]";
var result = {};
data.replace(/(\w+)=(\w+)/g, function(_, left, right) { result[left] = right; })
console.log(result);
I am trying to add an object in an array to an item in a JSON object.
The result I am looking for is:
{ "AvailableFacets":[ "color", "sheenlevel" ],
"Selections":[
{ "Facet":"color", "Value":"red" },
{ "Facet":"color", "Value":"blue" }
]
}
but I get the error "TypeError: myJsonObject.Selection.push is not a function" when doing the following:
var testJson = function () {
var myJsonObject = $.parseJSON('{"AvailableFacets":["color", "sheenlevel"]}');
myJsonObject.Selection = "[]";
var newObject1 = $.parseJSON('{"Facet":"color", "Value":"red"}');
var newObject2 = $.parseJSON('{"Facet":"color", "Value":"blue"}');
myJsonObject.Selection.push(newObject1);
return myJsonObject;
};
What am I doing wrong?
"[]" !== []. Did that help? You are using the wrong types. Also you are looking for an output with "Selections" but you are attempting to define "Selection", but I assume that is a typo. This should work:
myJsonObject.Selection = [{"Facet":"color", "Value":"red"},{"Facet":"color", "Value":"blue"}];
But if you wanted to parse a string of JSON as JSON then just change
myJsonObject.Selection = "[]";
to:
myJsonObject.Selection = [];
i been tryi'n to use angular nowadays. i'm just hoping if someone could help me why i cant fetch localstorage data like the below codes. thanks
[
{
"name": "firstname",
"email": "email#yahoo.com"
}
]
service.js
getItem: function (item) {
var temp = localStorage.getItem(item);
if (!temp){
return [];
}
return JSON.Parse(temp);
}
controller.js
profile.push({
name: 'firstname',
email: 'rmail#yahoo.com'
});
localStorage.setItem('profiles', JSON.stringify(profile));
console.log(service.getItem('name') + ' : this should output the name');
console.log(service.getItem('email') + ' : this should output the email');
Can you please try this
var array = service.getItem('profiles');
for(var i=0;i<array.length;i++){
console.log(array[i].name, array[i].email)
}
To forego using parsing and deal with any of this you can try a factory I recently created, it let's you store and retrieve arrays and objects. Also it let's you bind to $scope.variables:
https://github.com/agrublev/Angular-localStorage
I'm, Having trouble passing JSON data into the Jstree.
Following is my Jstree
$("#tree")
.bind("open_node.jstree", function (event, data) {
console.log(data.rslt.obj.attr("id"));
})
.jstree({
"plugins" : ["themes", "json_data", "ui"],
"json_data" : {
"data": [{"data":'Top Level - ',"state":'closed',"attr":{"seq_no":341386}}],
"state" : "open",
"ajax": {
"type": 'POST',
"dataType": 'json',
"data": {"action": 'getChildren'},
"url": function (node) {
var nodeId = node.attr('seq_no');
return 'ajax/test.jsp?seq_no=' + 341386;
},
"success": function (new_data) {
return new_data;
}
}
}
});
And test.jsp looks like this:
RecordCollection result=null;
PlsqlSelectCommand selCmd = null;
String sErrorMsg = "";
String sqlSelect;
OutputFormat format = new OutputFormat( doc ); //Serialize DOM
StringWriter xmlOut = new StringWriter(); //Writer will be a String
XMLSerializer serial = new XMLSerializer( xmlOut, format );
serial.serialize(doc);
JSONObject jsonObject = XML.toJSONObject(xmlOut.toString());
<%=jsonObject%>
The problem is JSON data isnt passed into the jstree. thus I cant set my children nodes. I've tried so hard but can't get this working.
Is there something else I need to do in order to pass this JSON string into the jstree.
My jstree works when the data feed is hard-coded.
Any help is much appreciated.