I want to use a JSON object to build a datatable, but I receive the following error:
after that, the browser shows :
I was trying by hours everthing that the url in the warning message says.. but i didnt figure out.
The JSON object is retrieved by a servlet named MyJson. This JSon has the following appareance.
{
"data":[
["NAME: Name1","DIRECTION: Salida","CHARGED: 15","AFFORDED: 15"],
["NAME: Name2","DIRECTION: Salida","CHARGED: 4","AFFORDED: 4"],
["NAME: Name3","DIRECTION: Entrada","CHARGED: 4","AFFORDED: 4"],
["NAME: Name4","DIRECTION: Salida","CHARGED: 1","AFFORDED: 0"],
["NAME: Name5","DIRECTION: Entrada","CHARGED: 15","AFFORDED: 15"],
["NAME: Name6","DIRECTION: Entrada","CHARGED: 10","AFFORDED: 10"],
["NAME: Name7","DIRECTION: Entrada","CHARGED: 15","AFFORDED: 15"],
["NAME: Name8","DIRECTION: Entrada","CHARGED: 3","AFFORDED: 3"],
["NAME: Name9","DIRECTION: Entrada","CHARGED: 15","AFFORDED: 15"]
]
}
And this is my javascript
<script>
$(document).ready(function() {
var tableEntityList = $('#testable').DataTable({
"processing": true,
"scrollY":"500px",
"scrollCollapse": true,
"paging":false,
"serverSide":true,
"ajax":"./MyJson",
"columns": [
{ "data":'NAME' },
{ "data":'DIRECTION' },
{ "data":'CHARGED' },
{ "data":'AFFORDED' }
]
});
})
</script>
<body>
<table class="display responsive nowrap" id="testable" cellspacing="0">
<thead>
<th>NAME</th>
<th>DIRECTION</th>
<th>CHARGED</th>
<th>AFFORDED</th>
</thead>
<tbody>
</tbody>
</table>
Can you tell me what am i doing wrong?
Thank you.
You'll need to process your data before passing it to DataTables and you can do this within dataSrc. Check this JSFiddle:
let jsonData = {
"data": [
["NAME: Name1", "DIRECTION: Salida", "CHARGED: 15", "AFFORDED: 15"],
["NAME: Name2", "DIRECTION: Salida", "CHARGED: 4", "AFFORDED: 4"],
["NAME: Name3", "DIRECTION: Entrada", "CHARGED: 4", "AFFORDED: 4"],
["NAME: Name4", "DIRECTION: Salida", "CHARGED: 1", "AFFORDED: 0"],
["NAME: Name5", "DIRECTION: Entrada", "CHARGED: 15", "AFFORDED: 15"],
["NAME: Name6", "DIRECTION: Entrada", "CHARGED: 10", "AFFORDED: 10"],
["NAME: Name7", "DIRECTION: Entrada", "CHARGED: 15", "AFFORDED: 15"],
["NAME: Name8", "DIRECTION: Entrada", "CHARGED: 3", "AFFORDED: 3"],
["NAME: Name9", "DIRECTION: Entrada", "CHARGED: 15", "AFFORDED: 15"]
]
};
$(document).ready(function() {
var tableEntityList = $('#testable').DataTable({
"processing": true,
"scrollY": "500px",
"scrollCollapse": true,
"paging": false,
"ajax": {
type: 'POST',
dataType: 'json',
url: '/echo/json/',
data: {
json: JSON.stringify(jsonData)
},
dataSrc: function(json) {
var properData = [];
$.each(json.data, function(k, v) {
var returnObject = {};
$.each(v, function(a, b) {
var elArr = b.split(":");
returnObject[elArr[0].trim()] = elArr[1].trim()
});
properData.push(returnObject)
});
return properData;
}
},
"columns": [{
"data": 'NAME'
}, {
"data": 'DIRECTION'
}, {
"data": 'CHARGED'
}, {
"data": 'AFFORDED'
}]
});
});
Please note though that serverSide will not work!
If you use columns.data, it means your JSON array should already have fields mapped with respective field name NAME,DIRECTION, CHARGED and AFFORDED like this :
var dataSet = [
{ "NAME": "Name1", "DIRECTION": "Salida", "CHARGED": 15, "AFFORDED": 15 },
{ "NAME": "Name2", "DIRECTION": "Salida", "CHARGED": 4, "AFFORDED": 4 },
{ "NAME": "Name3", "DIRECTION": "Entrada", "CHARGED": 4, "AFFORDED": 4 },
{ "NAME": "Name4", "DIRECTION": "Salida", "CHARGED": 1, "AFFORDED": 0 },
{ "NAME": "Name5", "DIRECTION": "Entrada", "CHARGED": 15, "AFFORDED": 15 },
{ "NAME": "Name6", "DIRECTION": "Entrada", "CHARGED": 10, "AFFORDED": 10 },
{ "NAME": "Name7", "DIRECTION": "Entrada", "CHARGED": 15, "AFFORDED": 15 },
{ "NAME": "Name8", "DIRECTION": "Entrada", "CHARGED": 3, "AFFORDED": 3 },
{ "NAME": "Name9", "DIRECTION": "Entrada", "CHARGED": 15, "AFFORDED": 15}
];
Check this fiddle
In your case you want to have only data without field name since you are using embedded array, you should have something like :
var dataSet = [
["Name1", "Salida", "15", "15"],
["Name2", "Salida", "4", "4"],
["Name3", "Entrada", "4", "4"],
["Name4", "Salida", "1", "0"],
["Name5", "Entrada", "15", "15"],
["Name6", "Entrada", "10", "10"],
["Name7", "Entrada", "15", "15"],
["Name8", "Entrada", "3", "3"],
["Name9", "Entrada", "15", "15"]
];
You can change columns.data to column.title to set the data title directly for each array item index. Check this fiddle
As you have included field name in your data. The best is to modify your JSON input source to return something directly usable. If you can't doing it, you can modify the data you receive and reorganize it by removing the embedded field name and then initialize datatables with the new modified input.
annoyingmouse approach solved my problem.
My final call is:
var tableEntityList = $('#testable').DataTable({
"processing": true,
"scrollY":"500px",
"scrollCollapse": true,
"paging":false,
"serverSide":true,
"ajax":{
url:"./MyJson",
dataSrc: function(json) {
var properData = [];
$.each(json.data, function(k, v) {
var returnObject = {};
$.each(v, function(a, b) {
var elArr = b.split(":");
returnObject[elArr[0].trim()] = elArr[1].trim()
});
properData.push(returnObject)
});
return properData;
}
},
"columns": [
{ "data":'NAME' },
{ "data":'DIRECTION' },
{ "data":'CHARGED' },
{ "data":'AFFORDED' }
]
});
Related
I have an array of object with data :
[
{
"id": 1,
"milestone": "Generated",
"phase_id": 1,
"phase": "ICP Assessment",
"activity_id": 1,
"activity": "Data Analysis",
"start_date": "2018-06-12T18:30:00.000Z",
"timeline": "3 weeks",
"responsibility": "2",
"status": "Not Started"
},
{
"id": 1,
"milestone": "Generated",
"phase_id": 1,
"phase": "ICP Assessment",
"activity_id": 2,
"activity": "Territory re-alignment",
"start_date": "2018-06-27T18:30:00.000Z",
"timeline": "2 Weeks",
"responsibility": "2",
"status": "Not Started"
},
{
"id": 2,
"milestone": "Identified",
"phase_id": 2,
"phase": "Pilot Planning",
"activity_id": 3,
"activity": "Existing ICP Discussion",
"start_date": "2018-06-27T18:30:00.000Z",
"timeline": "2 Weeks",
"responsibility": "2",
"status": "Not Started"
}
]
I want to convert this data into something like:
[
{
"id": 1,
"milestone": "Generated",
"phase":[
{
"phase_id": 1,
"phase": "ICP Assessment",
"activity"[
{
"activity_id": 1,
"activity": "Data Analysis",
"start_date": "2018-06-12T18:30:00.000Z",
"timeline": "3 weeks",
"responsibility": "2",
"status": "Not Started"
},
{
"activity_id": 2,
"activity": "Territory re-alignment",
"start_date": "2018-06-27T18:30:00.000Z",
"timeline": "2 Weeks",
"responsibility": "2",
"status": "Not Started"
}
]
}
]
},
{
"id": 2,
"milestone": "Identified",
"phase":[
{
"phase_id": 2,
"phase": "Pilot Planning",
"activity"[
{
"activity_id": 3,
"activity": "Existing ICP Discussion",
"start_date": "2018-06-27T18:30:00.000Z",
"timeline": "2 Weeks",
"responsibility": "2",
"status": "Not Started"
}
]
}
]
}
]
using node.js. just want a simple solution may be using for loop. Any help would be appreciated.
Array reduce method is there to rescue
The flow is as follows. In the array reduce initially pass an empty array in the thisArg.IN the empty array check if there exist an object whose Id is id in the main array.If not then create a new object and push to the empty array
let newData = date.reduce(function (acc, curr) {
//using findIndex to test if there exist an object whose id is same as id of main
//array of objects
let getIdIndex = acc.findIndex(function (item) {
return item.id === curr.id;
})
// if the id does not exist then create a new object with these values
if (getIdIndex === -1) {
let newObj = {
id: curr.id,
milestone: curr.milestone,
phase: [{
phase_id: curr.phase_id,
phase: curr.phase,
activity: [{
activity_id: curr.activity_id,
activity: curr.activity,
start_date: curr.start_date,
timeline: curr.timeline,
responsibility: curr.responsibility,
status: curr.status
}]
}]
}
acc.push(newObj)
}
else{
// now if the id exist you need to check if therse exist an object
// where phase id matches, then same as the above logic
let getPhaseIdIndex = acc[getIdIndex].phase.findIndex(function(item){
return item.phase_id === curr.phase_id;
})
if(getPhaseIdIndex === -1){
acc[getIdIndex].phase.push({
phase_id: curr.phase_id,
phase: curr.phase,
activity: [{
activity_id: curr.activity_id,
activity: curr.activity,
start_date: curr.start_date,
timeline: curr.timeline,
responsibility: curr.responsibility,
status: curr.status
}]
})
}
else{
acc[getIdIndex].phase[getPhaseIdIndex].activity.push(
{
activity_id: curr.activity_id,
activity: curr.activity,
start_date: curr.start_date,
timeline: curr.timeline,
responsibility: curr.responsibility,
status: curr.status
}
)
}
}
return acc;
}, []) // this empty array is thisArg, new values will be added to this
console.log(newData)
Get the full code with working demo here
var cart =
[
{
"Items": "",
"category": "",
"contents":
[
{
"Apple iPhone": "222",
"French": "Bounjour",
"id": 1234,
"icon": "/images/bg.jpg",
"callback": "use()",
"pricetag":"false"
}
]
},
{
"Items": "No 2",
"category": "2nd",
"contents":
[
{
"redmi": "333",
"French": "some text",
"id": 345787,
"icon": "/images/bg.jpg",
"callback": "use()",
"pricetag":"true"
},
{
"samsung": "333",
"French": "some text",
"id": 86787876,
"icon": "/images/bg.jpg",
"callback": "use()",
"pricetag":"disabled"
}
]
}
];
Can anyone help me to get the "id" value and "pricetag" value from the above JSON ? It is nested one and can be in different format. But in each, i need to extract id value and pricetag value. I tried so many things but not getting exact output. Can someone please help me ? Sorry for the bad formatting ..
use .map() to iterate and return the desired keys :
var cart = [{
"Items": "",
"category": "",
"contents": [{
"Apple iPhone": "222",
"French": "Bounjour",
"id": 1234,
"icon": "/images/bg.jpg",
"callback": "use()",
"pricetag": "false"
}]
},
{
"Items": "No 2",
"category": "2nd",
"contents": [{
"redmi": "333",
"French": "some text",
"id": 345787,
"icon": "/images/bg.jpg",
"callback": "use()",
"pricetag": "true"
},
{
"samsung": "333",
"French": "some text",
"id": 86787876,
"icon": "/images/bg.jpg",
"callback": "use()",
"pricetag": "disabled"
}
]
}
];
let values = cart.map((e) => {
return e.contents.map((a) => {
return {
id: a.id,
pricetag: a.pricetag
}
})
})
console.log(values)
This should work for you :
var items=[];
var some_var;
const map1 = cart.map(function(item){
const map2=item.contents.map(function(contentItem){
//some_var=contentItem.id;
return{
pricetag:contentItem.pricetag,
id:contentItem.id
}
})
items=items.concat(map2)
});
console.log(items);
Sample output:
[ { pricetag: 'false', id: 1234 },
{ pricetag: 'true', id: 345787 },
{ pricetag: 'disabled', id: 86787876 } ]
I want to remove object based on conditional check from the JSON object using angularjs/jQuery.
I tried with below code but output is not as expected.
Demo: https://plnkr.co/edit/EWwbETITqn7G79Xypt0g?p=preview
angular.module('ui.bootstrap.demo').controller('DataCtrl', function ($scope) {
$scope.responseData = {
"data": [{
"name": "Machine", "quantity": 20, "snVal": 22,
"machine1": [{ "id": 2009, "machineName": "ASD1", "trackID": "34219", "status": "delivered" },
{ "id": 27893, "machineName": "PX20AA", "trackID": "3422", "status": "avail" }],
"machine2": [{ "id": 1023, "machineName": "XY22", "trackID": "1345", "status": "avail" },
{ "id": 1233, "machineName": "PP3DF", "trackID": "112", "status": "delivered" }
]
}]
}
console.log("R1 :: " + JSON.stringify($scope.responseData));
$scope.newResponse = $.grep($scope.responseData.data, function (element, index) { return element.status == "delivered" }, true);
console.log("R2 after removing elements:: " + JSON.stringify($scope.newResponse));
});
Try this,
let responseData = { "data": [{ "name": "Machine", "quantity": 20, "snVal": 22, "machine1": [{ "id": 2009, "machineName": "ASD1", "trackID": "34219", "status": "delivered" }, { "id": 27893, "machineName": "PX20AA", "trackID": "3422", "status": "avail" }], "machine2": [{ "id": 1023, "machineName": "XY22", "trackID": "1345", "status": "avail" }, { "id": 1233, "machineName": "PP3DF", "trackID": "112", "status": "delivered" }] }] } ;
let newResponse = responseData;
newResponse.data.forEach(function (item) {
for (j in item) {
if (j.includes("machine")) {
//better you check if type is array, using Object.prototype.toString.call(j) === "[object Array]"
item[j] = item[j].reduce(function (acc, machine) {
if (machine.status !== "delivered"){
acc.push(machine);
}
return acc;
}, []);
}
}
})|
console.log(newResponse);
Here we are just iterating through all objects in the data field. Since properties like machine1, machine2 is an array, we iterate through it and filter all machines which are not delivered.
You must note that I have used a for-in loop and array reduce() method here.
I'm trying to merge two javascript objects that has inside of it arrays and other elements. Let me give you an example.
{
"addresses": [
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai1#gmail.com"
},
{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839332"
},
{
"type": "email",
"tags": [
"Mãe"
],
"address": "johndoemae1#gmail.com"
},
{
"type": "email",
"tags": [
"Aluno"
],
"address": "johndoealuno1#gmail.com"
}
],
"class": [
"Sala 1",
"Sala 2",
"Sala 3"
],
"fullname": "John Doe 1",
"eid": "2",
"invisible": true,
"see_all": false
},
{
"addresses": [
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai2#gmail.com"
},
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai3#gmail.com"
},
{
"type": "phone",
"tags": [
"Pai"
],
"address": "5519985504400"
},
{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839333"
},
{
"type": "email",
"tags": [
"Mãe"
],
"address": "11 983340440"
},
{
"type": "email",
"tags": [
"Aluno"
],
"address": ""
}
],
"class": [
"Sala 4",
"Sala 5",
"Sala 6"
],
"fullname": "John Doe 1",
"eid": "2",
"invisible": false,
"see_all": true
}
As you can see, we can have arrays inside with some strings in those arrays.
I've tried to do it using some recursion, but had no success.
function mergeObjects(target, source){
var item, tItem, o, idx;
if (typeof source == 'undefined') {
return source;
} else if (typeof target == 'undefined') {
return target;
}
for (var prop in source) {
item = source[prop];
//check if the first element is indeed an object.
if (typeof item == 'object' && item !== null) {
//if the first item is an array
if (_.isArray(item) && item.length) {
//dealing with array of primitives
if (typeof item[0] != 'object') {
item.forEach(function(conteudo){
//push to the target all the elements;
target[prop].push(conteudo);
});
}else{
//dealing with array of objects;
for(var attr in item){
idx = {};
tItem = target[attr]
mergeObjects(tItem,item);
}
}
}//if its a normal object
else {
// deal with object
mergeObjects(target[prop],item);
}
} else {
// item is a primitive, just copy it over
target[prop] = item;
}
}
return target;
}
I Expected this:
{
"addresses": [
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai2#gmail.com"
},
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai3#gmail.com"
},
{
"type": "phone",
"tags": [
"Pai"
],
"address": "5519985504400"
},
{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839333"
},
{
"type": "email",
"tags": [
"Mãe"
],
"address": "11 983340440"
},
{
"type": "email",
"tags": [
"Aluno"
],
"address": ""
}
],
"class": [
"Sala 4",
"Sala 5",
"Sala 6",
"Sala 1",
"Sala 2",
"Sala 3"
],
"fullname": "John Doe 1",
"eid": "2",
"invisible": true,
"see_all": false
}
"fullname": "John Doe 1",
"eid": "1234",
"classes": [
"Sala 1",
"Sala 2",
"Sala 3",
"Sala 4",
"Sala 5",
"Sala 6"
],
"addresses": [{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839332"
}, {
"type": "email",
"tags": [
"Mãe"
],
"address": "johndoemae1#gmail.com"
}, {
"type": "email",
"tags": [
"Aluno"
],
"address": "johndoealuno1#gmail.com"
}, {
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai2#gmail.com"
}, {
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai3#gmail.com"
}, {
"type": "phone",
"tags": [
"Pai"
],
"address": "5519985504400"
}, {
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839333"
}],
"invisible": true,
"see_all": true
}
But what i got was this, as you can see, some email elements are missing.
{
"addresses": [
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai2#gmail.com"
},
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai3#gmail.com"
},
{
"type": "phone",
"tags": [
"Pai"
],
"address": "5519985504400"
},
{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839333"
},
{
"type": "email",
"tags": [
"Mãe"
],
"address": "11 983340440"
},
{
"type": "email",
"tags": [
"Aluno"
],
"address": ""
}
],
"class": [
"Sala 4",
"Sala 5",
"Sala 6",
"Sala 1",
"Sala 2",
"Sala 3"
],
"fullname": "John Doe 1",
"eid": "2",
"invisible": true,
"see_all": false
}
Order of elements is not a problem.
Which point of the recursion tree did i miss?
Short answer: you have to merge the arrays yourself first. Or you can use something nice like Lodash's mergeWidth Ramda's R.mergeWith (see below for sample code for Ramda).
You want something like the native Object.assign (limited browser support, so it will have to be polyfilled) or whatever merge your JS library has.
The problem you're running into is how all of these merge implementations handle duplicate keys. Usually the key will be set to the last value passed in:
var thing1 = {
someProperty: 'foo'
};
var thing2 = {
someProperty: 'bar'
};
var result1 = Object.assign({}, thing1, thing2);
// -> {someProperty: 'bar'} (set to the last value passed in, in thing2)
var result2 = Object.assign({}, thing2, thing1);
// -> {someProperty: 'foo'} (again set to the last value passed in, in thing1)
// Make the Stackoverflow snippet display the output.
document.body.innerHTML += JSON.stringify(result1, null, 2);
document.body.innerHTML += '<br>' + JSON.stringify(result2, null, 2);
Works the same way with your example, except the property is a more complicated array. It's just being set to the last array passed in:
var thing1 = {
someList: [1, 2, 3]
};
var thing2 = {
someList: [4, 5, 6]
};
var result = Object.assign({}, thing1, thing2);
// -> {someList: [4, 5, 6]}
// Make the Stackoverflow snippet display the output.
document.body.innerHTML = JSON.stringify(result, null, 2);
If you use something nice like Ramda, you can merge and specify a function to control the behavior when keys are equal, like in your case. Here we can check if the value is an array, then concatenate if so. Otherwise, return the latest value (as described above):
var thing1 = {someList: [1, 2, 3]};
var thing2 = {someList: [4, 5, 6]};
var dupeMergeFn = function(a, b) {
return (Array.isArray(a)) ? a.concat(b) : b;
};
var result = R.mergeWith(dupeMergeFn, thing1, thing2);
// Make the Stackoverflow snippet display the output.
document.body.innerHTML = JSON.stringify(result, null, 2);
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.19.1/ramda.min.js"></script>
I have a problem. How can I get refcat in that Json object in javascript. Please find the below code snippet.
{
"request": {
"operation": "Viviendas",
"languague": "es",
"idMunicipio": "5",
"idVia": "196",
"idProvincia": "28",
"pc1": "9115501",
"pc2": "VK6891N",
"numero": "1"
},
"created_http": "Thu Oct 01 09:33:58 GMT+01:00 2015",
"created_server": 20151001092458,
"data": [
{
"portal": "",
"escaleras": [
{
"escalera": "1",
"plantas": [
{
"planta": "00",
"puertas": [
{
"puerta": "01",
"refCat": "9115501VK6891N0001GB",
"superficieVivienda": 60.0,
"superficieTotal": 60.0
}
]
},
{
"planta": "01",
"puertas": [
{
"puerta": "01",
"refCat": "9115501VK6891N0002HZ",
"superficieVivienda": 154.0,
"superficieTotal": 170.0
}
]
},
{
"planta": "02",
"puertas": [
{
"puerta": "01",
"refCat": "9115501VK6891N0003JX",
"superficieVivienda": 161.0,
"superficieTotal": 177.0
}
]
}
]
}
]
}
],
"page": "1",
"pagesTotal": "1",
"status_code": "200",
"status_text": "OK"
}
I tried to fix this with the below method.
alert(json.data[0].escaleras[0].plantas[0].puertas[i].refCat);
But it doesn't work. Can someone please help me with this?
Hi you would have to iterate through the json object to get the data.
Use the following code.
json.data.forEach(function (object) {
object.escaleras.forEach(function (innerObject) {
innerObject.plantas.forEach(function (innerMostObject) {
innerMostObject.puertas.forEach(function (refCatObject) {
console.log(refCatObject.refCat)
})
})
})
})
The problem is
alert(json.data[0].escaleras[0].plantas[0].puertas[i].refCat);
i is causing the problem...... ^
it should be 0.
alert(json.data[0].escaleras[0].plantas[0].puertas[0].refCat);
var json = {
"request": {
"operation": "Viviendas",
"languague": "es",
"idMunicipio": "5",
"idVia": "196",
"idProvincia": "28",
"pc1": "9115501",
"pc2": "VK6891N",
"numero": "1"
},
"created_http": "Thu Oct 01 09:33:58 GMT+01:00 2015",
"created_server": 20151001092458,
"data": [
{
"portal": "",
"escaleras": [
{
"escalera": "1",
"plantas": [
{
"planta": "00",
"puertas": [
{
"puerta": "01",
"refCat": "9115501VK6891N0001GB",
"superficieVivienda": 60.0,
"superficieTotal": 60.0
}
]
},
{
"planta": "01",
"puertas": [
{
"puerta": "01",
"refCat": "9115501VK6891N0002HZ",
"superficieVivienda": 154.0,
"superficieTotal": 170.0
}
]
},
{
"planta": "02",
"puertas": [
{
"puerta": "01",
"refCat": "9115501VK6891N0003JX",
"superficieVivienda": 161.0,
"superficieTotal": 177.0
}
]
}
]
}
]
}
],
"page": "1",
"pagesTotal": "1",
"status_code": "200",
"status_text": "OK"
};
alert(json.data[0].escaleras[0].plantas[0].puertas[0].refCat);
assuming you had a div with the id 'test' , this works. I'd guess it's your indexing logic generating the [i] on 'puertas' that's not correct, or perhaps your 'json' object isn't actually a JSON Object?
example jsfiddle https://jsfiddle.net/p6udz7ao/
copy of example below:
$(document).ready(function () {
var jsonString = '{ "request": { "operation": "Viviendas", "languague": "es" ,"idMunicipio":"5","idVia":"196","idProvincia":"28","pc1":"9115501","pc2":"VK6891N","numero":"1" }, "created_http": "Thu Oct 01 09:33:58 GMT+01:00 2015", "created_server": 20151001092458, "data": [ { "portal": "", "escaleras": [ { "escalera": "1", "plantas": [ { "planta": "00", "puertas": [ { "puerta": "01", "refCat": "9115501VK6891N0001GB", "superficieVivienda": 60.0, "superficieTotal": 60.0 } ] } , { "planta": "01", "puertas": [ { "puerta": "01", "refCat": "9115501VK6891N0002HZ", "superficieVivienda": 154.0, "superficieTotal": 170.0 } ] } , { "planta": "02", "puertas": [ { "puerta": "01", "refCat": "9115501VK6891N0003JX", "superficieVivienda": 161.0, "superficieTotal": 177.0 } ] } ] } ] } ], "page" : "1", "pagesTotal" : "1", "status_code" : "200", "status_text" : "OK" }';
var jsonObject = JSON.parse(jsonString);
var refcattext = jsonObject.data[0].escaleras[0].plantas[0].puertas[0].refCat;
$('#test').text(refcattext);
});