My code snippet for nested json object:
JsonArray arr = jsonObject.getAsJsonArray("data");
for(int i = 0; i < arr.size(); i++) {
String _Id = arr.get(i).getAsJsonObject().get("_id").getAsString();
String Name = arr.get(i).getAsJsonObject().get("name").getAsString();
int Trips = arr.get(i).getAsJsonObject().get("trips").getAsInt();
}
You can parse JSON using JSON.parse();
let exampleJSON = '{ "id": 245, "name": "Mike" }';
const obj = JSON.parse(exampleJSON);
console.log(obj.id); // 245
Related
var res =
{
"response": {
"data": {
"profilesearchsnippet": [
[
{
"profileInfo": {
"firstname": "Sundar",
"lastname": "v"
},
"roleInfo": {
"defaultphotoid": 94
}
}
],
[
{
"profileInfo": {
"firstname": "ghg",
"lastname": "vbhvh"
},
"roleInfo": {
"defaultphotoid": 171
}
}
]
]
}
}
$scope.profileData = [];
I have a response var res . I need to pass my defaultphotoid to another request and form URL for displaying the image. which I pushed in $scope.images and I need to display all list of images along with respective firstname and lastname. But I couldnt do it. somewhere I am lacking it.
I pushed firstname and lastname in array by creating object in $scope.profileData
$scope.searchData = res.response.data.profilesearchsnippet;
for (var i = 0; i < searchData.length; i++) {
$scope.profileData.push({
'fname':searchData[0].profileInfo.firstname,
'lname':searchData[0].profileInfo.lastname
});
}
The above $scope.profileData [] has exact values what I am pushing , But I could push the values of images from $scope.images into this.
$scope.profileData.push({'image': images[i]});
what happen in above case is the first array has fname and lname object and second array object has image.
When you push into an array, it is added as a separate object in the array. If you want the image to be added along with fname and lname, add it in the loop itself like :
$scope.profileData.image = images[i];
If you already has the $scope.images array, You can add the image too in the same for loop you are using to add lName and fName like this :
for (var i = 0; i < searchData.length; i++){
$scope.profileData.push({'fname':searchData[i].profileInfo.firstname,'lname':searchData[i].profileInfo.lastname, 'image':$scope.images[i]});
}
try like this
$scope.searchData = res.response.data.profilesearchsnippet;
for (var i = 0; i < searchData.length; i++){
$scope.profileData.push({'fname':searchData[0].profileInfo.firstname,'lname':searchData[0].profileInfo.lastname, 'image':''});
}
Then:-
for (var i = 0; i < $scope.images.length; i++){
$scope.profileData[i].image = $scope.images[i];
}
I have this array:
datas = [{USR_Website: "http://domain.com"}, {USR_FirstName: "Alex", USR_LastName: "Black", USR_Email: "ab#domain.com"}, {USR_Password: "fc6e6d7c3a72b46a69e2f8f594a775acef6b3ba1"}, {USR_Country: "CA", USR_TimeZone: "America/New_York"}];
How can I convert this array to have:
datas = {USR_Website: "http://domain.com", USR_FirstName: "Alex", USR_LastName: "Black", USR_Email: "ab#domain.com", USR_Password: "fc6e6d7c3a72b46a69e2f8f594a775acef6b3ba1", USR_Country: "CA", USR_TimeZone: "America/New_York"};
I want it like this because it more easy to make datas.USR_Email than
datas[1].USR_Email.
Or perhaps, another solution ?
Thanks.
Try something like this:
var target = {};
for (var i = 0, l = datas.length; i < l; i++) {
for (var key in datas[i]) {
target[key] = datas[i][key];
}
}
You can now access the data as property of target, eg. target.USR_Country.
I got a Json file like;
{
"Cities": [
{
"Name": "London",
"Country": "UK"
},
{
"Name": "Rome",
"Country": "ITA"
},
{
"Name": "Antalya",
"Country": "TR"
}
]
}
How can I get City Names as an array like ["London","Rome","Antalya"] without doing;
var tempJSON = JSON.parse(jsonCities);
var arrayCityNames = [];
for (var i = 0; i < tempJSON.Table.length; i++){
arrayCityNames[i] = tempJSON.Table[i].Name;
}
if tempJSON was a Dataset we could use getColumnArray
arrayCityNames = Data.Dataset.getColumnArray("Name");
Is there any built in method to do this for parsed JSON's?
Please keep in mind that the question is related to Smartface.io Framework, not jquery itself
Try this:
var tempJSON = JSON.parse(jsonCities);// here you load your JSON
var arrayCityNames = []; // your output array
var cityArray = tempJSON['Cities']; // enter Cities array
for (var i = 0; i <cityArray.length; i++){ // iterate over your list
arrayCityNames.push(cityArray[i]['Name']); // add to list name of your city list
}
I have An Array With Dictionary example :
[{
"CATEGORYNAME" = "name0";
"CATEGORYSUBID" = 2;
"ID" = 1;
}, {
"CATEGORYNAME" = "name1";
"CATEGORYSUBID" = 2;
"ID" = 2;
}, {
"CATEGORYNAME" = "name2";
"CATEGORYSUBID" = 0;
"ID" = 3;
}]
I Used to Filter it in Objective C Like this
JSON_data = [[[Global SharedData]Categorys] filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:#"(CATEGORYSUBID == %#)", Filter]];
i tried To Use Array Filter but not Succeed
var JSON_data = Global.SharedData().Categorys
JSON_data = JSON_data.filter( ?????
JSON_data is has all data i have print it with Printin
Filtering a dictionary would be simple like below. We filter ages that are below 30.
var visitors = [["age" : 22], ["age" : 41], ["age" : 23], ["age" : 30]]
var filteredVisitors = visitors.filter({
$0["age"] < 30 //access the value to filter
})
println(filteredVisitors)
//[["age" : 22], ["age" : 23]]
More info here: Filtering a Swift Array of Dictionaries or Object property
This looks like just a simple question in how to translate. Your best option is to go through the various Sessions from WWDC, but a literal translation is:
let categories = Global.SharedData().Categorys()
JSON_data = categories.filter({
if let subid = $0["CATEGORYSUBID"] {
return subid == filter
} else {
return false
}
})
If that doesn't work you'll need to post a lot more information about how Global, SharedData, Categorys and JSON_data.
I want to parse a Json File where all Json Arrays have the same Name:
[
{
"mobileMachine":{
"condition":"GOOD",
"document":"a",
"idNr":"ce4f5a276a55023efced9c6a4b02bf4fcff04c06b4338467c8679770bff32313f7f372b5ec2f7527dad0de47d0fb117e"
}
},
{
"mobileMachine":{
"condition":"GOOD",
"document":"b",
"idNr":"ce4f5a276a8e023efced9c6a4b02bf4fcff04c06b4338467c8679770bff32313f7f372b5ec2f7527dad0de47d0fb217e"
}
},
...
]
So here is my little Code:
JSONArray json = new JSONArray(urlwhereIGetTheJson);
for (int count = 0; count < json.length(); count++) {
JSONObject obj = json.getJSONObject(count);
String condition = obj.getString("condition");
String document = obj.getString("document");
String idNr = obj.getString("idNr");
db.addMachine(new MachineAdapter(condition, document, idNr));
}
I hope u can show me how to parse the JSON File correctly. Thank you
I can't edit the JSON File. (The File include more than 300 mobilemachines. I have shorten this).
(Sorry for my English)
Edit: You are using the new JSONArray() constructor incorrectly. Have a look at the documentation. You can not directly pass the url there. You have to obtain it first, and then pass the json to the constructor.
The following piece of code does what you want to do:
JSONArray jsonArray = new JSONArray(json);
int numMachines = jsonArray.length();
for(int i=0; i<numMachines; i++){
JSONObject obj = jsonArray.getJSONObject(i);
JSONObject machine = obj.getJSONObject("mobileMachine");
String condition = machine.getString("condition");
String document = machine.getString("document");
String idNr = machine.getString("idNr");
db.addMachine(new MachineAdapter(condition, document, idNr));
}
You forgot to obtain the "mobileMachine" json object, and tried to access condition/document/idNr directly.
If you have control over the XML, you could make it smaller by removing the "mobileMachine" node:
[
{
"condition":"GOOD",
"document":"a",
"idNr":"ce4f5a276a55023efced9c6a4b02bf4fcff04c06b4338467c8679770bff32313f7f372b5ec2f7527dad0de47d0fb117e"
},
{
"condition":"GOOD",
"document":"b",
"idNr":"ce4f5a276a8e023efced9c6a4b02bf4fcff04c06b4338467c8679770bff32313f7f372b5ec2f7527dad0de47d0fb217e"
},
...
]
Change it to
JSONArray json = new JSONArray(jsonString);
for (int count = 0; count < json.length(); count++) {
JSONObject obj = json.getJSONObject(count).getJSONObject("mobileMachine");
String condition = obj.getString("condition");
String document = obj.getString("document");
String idNr = obj.getString("idNr");
db.addMachine(new MachineAdapter(condition, document, idNr));
}
You forgot "mobileMachine".