I wish to generate a DataTable with ajax response as the source. I convert an ArrayList of employees to JSON string using GSON libraries. The JSON string I get is
{
"sEcho": 3,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
{
"id": 1,
"firstName": "darsheet",
"lastName": "shah",
"city": "san jose",
"state": "ca",
"zip": 95112
},
{
"id": 2,
"firstName": "akshat",
"lastName": "shah",
"city": "ahmedabad",
"state": "gj",
"zip": 380061
}
]
}
But the sAjaxSource attribute requires JSON string in following format
{
"sEcho": 3,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
{
"0": 1,
"1": "darsheet",
"2": "shah",
"3": "san jose",
"4": "ca",
"5": 95112
},
{
"0": 2,
"1": "akshat",
"2": "shah",
"3": "ahmedabad",
"4": "gj",
"5": 380061
}
]
}
The Datatable code I use is
$(document).ready(function(){
$('#refresh').click(function(){
$('#emp').dataTable({
"sAjaxSource":".../ExploreDatatable/loadTableAjax"
});
});
});
How to remove this mismatch in JSON structure ?
Thanks.
As I know it's that you option of datatable is wrong. it should do it like this:
$('#emp').dataTable({
"sAjaxSource":".../ExploreDatatable/loadTableAjax",
"aoColumns": [{"mDataProp": "id"},
{"mDataProp": "firstName"},
.....the other column names, that should be mapped to the json key......
]
});
Related
I'm having this response
"countryitems": [
{
"1": {
"ourid": 1,
"title": "Afghanistan",
"code": "AF",
},
"2": {
"ourid": 2,
"title": "Albania",
"code": "AL",
},
"3": {
"ourid": 3,
"title": "Algeria",
"code": "DZ", },
"4": {
"ourid": 4,
"title": "Angola",
"code": "AO",
}
}
]
For question, I've put only 4 nodes where actually I've 150 around of nodes. I'm not getting how can I parse to get country names?
Here is a possible solution. You have to decode the response with jsonDecode and generating a map out of the response. If you iterate through the map, you can access the inner nodes.
import 'dart:convert';
var jsonString =
"""
{
"countryitems": [
{
"1": {
"ourid": 1,
"title": "Afghanistan",
"code": "AF"
},
"2": {
"ourid": 2,
"title": "Albania",
"code": "AL"
},
"3": {
"ourid": 3,
"title": "Algeria",
"code": "DZ"
},
"4": {
"ourid": 4,
"title": "Angola",
"code": "AO"
}
}
]
}
""";
void main() {
Map<String, dynamic> obj = json.decode(jsonString)['countryitems'][0];
// print out all country names in obj
for(int i = 1; i <= obj.length; i++) {
print(obj['$i']['title']);
}
}
'dart:convert' is good for simple cases.
But when you need true flexibility and less boilerplate, I'd suggest to use this library instead https://github.com/k-paxian/dart-json-mapper
I have the following json from a API i'm getting via requests.get().
Now I want to turn this json into a dict with json.loads() but the outcome is a list.
From https://www.w3schools.com/python/python_json.asp the outcome should be a dict... or am I wrong here? Thanks!
import requests
import json
r = requests.get('http://url/api,
auth=requests.auth.HTTPBasicAuth('user','pass'))
x = json.loads(r.text)
# type(x) >>> <class 'list'>
json:
[
{
"id": 400285,
"statusId": 1,
"deliveryAddress": {
"firstName": "Admin",
"lastName": "Admin",
"company": "Testshop 2",
"street": "Teststr. 1",
"houseNumber": "",
"additionalAddressInfo": "",
"postcode": "12345",
"city": "Testort",
"state": "Bremen",
"country": "Germany",
"countryIsoCode": "DE"
},
"billingAddress": {
"firstName": "Admin",
"lastName": "Admin",
}
},
{
"id": 400288,
"statusId": 1,
"deliveryAddress": {
"firstName": "Carolin",
"lastName": "Pr\u00f6ger",
"company": "",
"street": "teststra\u00dfe 12",
"houseNumber": "",
"additionalAddressInfo": "",
"postcode": "12345",
"city": "Bremen",
"state": "Bremen",
"country": "Germany",
"countryIsoCode": "DE"
},
"billingAddress": {
"firstName": "Carolin",
"lastName": "Pr\u00f6ger",
}
}
]
Edit1:
mydict= {}
for item in mylist:
mydict['ID'] = item['id']
mydict['statusID'] = item['statusId']
The data type returned from json.load is assigned depending of the data structure of the json file.
Your json file has [] (list structure).
But you need your json file to have {} (list structure).
It is because of your json response contains list not object.
I have this json response from db.
{"StudentId":"1","SubjectId":"1","Mark":"61"}{"StudentId":"1","SubjectId":"2","Mark":"75"}{"StudentId":"1","SubjectId":"3","Mark":"87"}{"StudentId":"2","SubjectId":"1","Mark":"82"}{"StudentId":"2","SubjectId":"2","Mark":"64"}{"StudentId":"2","SubjectId":"3","Mark":"77"}
I want convert as
{"StudentId":"1",
"Mark":[ "1":"61", "2":"75", "3":"87" ]
}
{"StudentId":"2",
"Mark":[ "1":"82", "2":"64", "3":"77" ]
}
By using this I want to generate a html table.
Are you by any chance using Couchbase? This transformation would be quite easy in N1QL. Here's the query:
SELECT data.StudentId, OBJECT v.SubjectId:v.Mark FOR v IN ARRAY_AGG(data) END as Mark
FROM [{"StudentId":"1","SubjectId":"1","Mark":"61"},
{"StudentId":"1","SubjectId":"2","Mark":"75"},
{"StudentId":"1","SubjectId":"3","Mark":"87"},
{"StudentId":"2","SubjectId":"1","Mark":"82"},
{"StudentId":"2","SubjectId":"2","Mark":"64"},
{"StudentId":"2","SubjectId":"3","Mark":"77"}] data
GROUP BY data.StudentId
Presumably you would be replacing the hard-coded array in the FROM clause with a query to get the data in the first place.
Here's the output:
[
{
"Mark": {
"1": "61",
"2": "75",
"3": "87"
},
"StudentId": "1"
},
{
"Mark": {
"1": "82",
"2": "64",
"3": "77"
},
"StudentId": "2"
}
]
{"StudentId":"1","SubjectId":"1","Mark":"61"},
{"StudentId":"1","SubjectId":"2","Mark":"75"},
{"StudentId":"1","SubjectId":"3","Mark":"87"},
{"StudentId":"2","SubjectId":"1","Mark":"82"},
{"StudentId":"2","SubjectId":"2","Mark":"64"},
{"StudentId":"2","SubjectId":"3","Mark":"77"}
In DB I have results table with attributes StudentId, SubjectId, Mark
I fetched the data and converted to JSON.
I finally want data in this format.
[
{
"Mark": {
"1": "61",
"2": "75",
"3": "87"
},
"StudentId": "1"
},
{
"Mark": {
"1": "82",
"2": "64",
"3": "77"
},
"StudentId": "2"
}
]
I have a JSON response as shown below (in array values). I want to validate whether the Keys (attributes) are properly retrieved in the Response. I would like to compare a list of expected values against the response values (only the first set of array values)
Eg:
"participants": [
{
"FirstName": "Kim",
"LastName": "Hykes",
"Street1": "ABC",
"Street2": "ABCD",
"City": "city1",
"State": "NJ"
}
{
"FirstName": "John",
"LastName": "David",
"Street1": "XYZ",
"Street2": "UXYZ",
"City": "city2",
"State": "NY"
}
]
Using JSONparser, the above JSON is parsed and the result will be:
participants[0].FirstName, participants[0].LastName,
participants[0].Street1, participants[0].Street2, participants[0].City,
participants[0].State, participants[1].FirstName,
participants[1].LastName,
participants[1].Street1, participants[1].Street2, participants[1].City,
participants[1].State
And it goes on until 50 participants
I would like to see whether all the required Keys are fetched from the database and would like to check only the first Array fields i.e. only in Participants[0].
Can anyone help me to figure the solution here?
Do you mean something like this, to validate whether the keys are in there, and in the correct order?
var response = {"participants": [
{
"FirstName": "Kim",
"LastName": "Hykes",
"Street1": "ABC",
"Street2": "ABCD",
"City": "city1",
"State": "NJ"
},
{
"FirstName": "John",
"LastName": "David",
"Street1": "XYZ",
"Street2": "UXYZ",
"City": "city2",
"State": "NY"
}
]
}
var keysNeeded = ['FirstName', 'LastName', 'Street1', 'Street2', 'City', 'State']
var i = 0;
for (key in response.participants[0]) {
if (key == keysNeeded[i]) {
console.log('all is OK with key '+key)
}
i++;
}
I am trying to utilize the JSON result of a GET request to my Li3 app, but I would like the result to be an array of the returned JSON objects, rather than an object of the JSON objects.
I have the following code in my view file (index.html.php):
print($todos->to('json'));
Which results in each row becoming a JSON object (good), but within an over-arching JSON object.
{
"1": {
"id": "1",
"title": "One",
"done": "0"
},
"2": {
"id": "2",
"title": "Two",
"done": "0"
},
"3": {
"id": "3",
"title": "Three",
"done": "0"
},
"4": {
"id": "4",
"title": "Four",
"done": "0"
}
}
I would like to get:
[
{
"id": "1",
"title": "One",
"done": "0"
},
{
"id": "2",
"title": "Two",
"done": "0"
},
{
"id": "3",
"title": "Three",
"done": "0"
},
{
"id": "4",
"title": "Four",
"done": "0"
}
]
Note: I've found that this was the case (array of objects) in commit "974469cf25db5cbab61f3e1ff172405f4635032e" of the lithium github project, but with anything after that commit, the result is an object of objects.
Try $todos->to('json', ['indexed' => false]), or, refer to the Media class for direct serialization of JSON without the template.
Todos::all(['return' => 'array'))->to('json'); works perfect with RecordSet too