Load multiple JSON object - json

I am trying to load multiple JSON object from one file, but my attempts failed.
Here is my code which run to error when I tried to load .JSON file.
$(document).ready(function(){
$.ajax({
url: "..data.json",
method: "GET",
success: function(data) {
// do something
},
error: function(data) {
console.log('error');
}
});
});
The file format what I am trying to load is the following:
[{"id_first":"1","data_first":"1"},{"id_first":"2","data_first":"2"}] [{"id_second":"1","data_second":"1"},{"id_second":"2","data_second":"2"}]
Is there any solultion for this problem? Thanks for helps in advance!

i think you need to change the json schema like this at the backend.
[
[{
"id_first": "1",
"data_first": "1"
}, {
"id_first": "2",
"data_first": "2"
}],
[{
"id_second": "1",
"data_second": "1"
}, {
"id_second": "2",
"data_second": "2"
}]
]
it more easier for you to process the data.

Related

Filing Bootstrap Datatable from remote JSON file

I am trying to fill the table (Bootstrap datatable) with the data from remote JSON file.
JSON file is located at https://ba.ekapija.com/company/tender-winner-json/103510/pobede-na-tenderima?hash=28cd4a0e334aec8f84a94f30bb340e7f
And this is the function I use:
$(document).ready( function() {
$('#twodotsmediatable').dataTable( {
"data": "https://ba.ekapija.com/company/tender-winner-json/103510/pobede-na-tenderima?hash=28cd4a0e334aec8f84a94f30bb340e7f",
"columns": [
{ "data": "tender" },
{ "data": "url" },
{ "data": "date" },
{ "data": "amount" },
{ "data": "company" },
{ "data": "address" }
]
} );
$('.dataTables_length').addClass('bs-select');
});
I have also tried with:
"ajax": "https://ba.ekapija.com/company/tender-winner-json/103510/pobede-na-tenderima?hash=28cd4a0e334aec8f84a94f30bb340e7f"
But with no luck in both cases. Please help me to find where am I making mistake.
You should always use ajax.url :
$('#twodotsmediatable').dataTable( {
ajax: {
url: 'https://ba.ekapija....'
},
columns: [ .. ]
})
You cannot overcome request blocking in the browser, but you can get the desired JSON through a serverside proxy. If your server support PHP a proxy.php could look like this :
<?
echo file_get_contents($_GET['url']);
?>
Get data via proxy :
$('#twodotsmediatable').dataTable( {
ajax: {
url: 'proxy.php?url=https://ba.ekapija....',
dataSrc: function(d) {
return d[0];
}
},
columns: [ .. ]
})
NB: Use of dataSrc is needed since the JSON seem to be on the form [[{ item, item, .. } ]]

Struggling to bind JSON data - JQuery Datatables

I am struggling to bind the JSON data into a Datatable. This is my Javascript code.
$.ajax({
type: "POST",
data: $('#searchForm').serialize(),
url: "/ReportingItem/Search",
success: function (data) {
$('#searchResults').DataTable({
data: data,
dataSrc: "",
columns: [
{ "data": "ID" },
{ "data": "ItemContent" }
]
});
},
error: function (xhr, ajaxOptions, thrownError) {
if (xhr.status == 404) {
alert(thrownError);
}
}
});
This is my JSON
"[{"ID":"3","ItemContent":"2nd Test"},{"ID":"4","ItemContent":"3rd Test"},{"ID":"9","ItemContent":"eeeeee"},{"ID":"11","ItemContent":"aaaa"}]"
You can see its a flat array. therefore I have used dataSRC="" so it doesn't look for "data" in the JSON
This is the error message I am getting
DataTables warning: table id=searchResults - Requested unknown
parameter 'ID' for row 0, column 0. For more information about this
error, please see http://datatables.net/tn/4
Any help on the Syntax would be helpful!
Thanks
Chris
Just add in the data line
$('#searchResults').DataTable({
data: JSON.parse(data),
....
});
JSON.parse
Just you have to check what json objects return in ReportingItem/Search.
as you mention:
"[{"ID":"3","ItemContent":"2nd Test"},{"ID":"4","ItemContent":"3rd Test"},{"ID":"9","ItemContent":"eeeeee"},{"ID":"11","ItemContent":"aaaa"}]"
in most case json format is camel notation format. it means object names start with lowercase. so you should make some changes in WebApiConfig or if its not api in a config which you set : ReportingItem/Search
add this to the method Register:
using System.Web.Mvc;
using System.Web.Routing;
public static void Register(HttpConfiguration config)
{
var Settings = config.Formatters.JsonFormatter.SerializerSettings;
Settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Settings.Formatting = Formatting.Indented;
//... other codes
}
at the end instead of
columns: [
{ "data": "ID" },
{ "data": "ItemContent" }
]
use this:
columns: [
{ "data": "iD" },
{ "data": "itemContent" }
]
hope its done.

I try to generate output the json string return from php but i cant do it

tthe following is my json string return from php and suggest me how i can get value from following json string.
[
{
"picid": "13",
"itemcode": "P-0000001",
"filename": "-1970-01-011.jpg",
"primarystatus": "active"
},
{
"picid": "16",
"itemcode": "P-0000001",
"filename": "dateArray1.jpg",
"primarystatus": "active"
},
{
"picid": "18",
"itemcode": "P-0000001",
"filename": "dateArray3.jpg",
"primarystatus": "active"
},
{
"picid": "19",
"itemcode": "P-0000001",
"filename": "dateArray4.jpg",
"primarystatus": "active"
}
]
//php
function returnALLPic()
{
if(isset($_POST['id']))
{
$data= $this->m_phone->getAllPictureInformation($_POST["id"]);
$ss= json_encode($data);
echo $ss;
}
}
//jquery
$.ajax({
type: 'POST',
url: 'http://localhost/tt/index.php/ad_access/c_r/returnALLPic',
data: 'id='+ei,
success: function(msg)
{
// console.log(JSONObject); // Dump all data of the Object in the console
$('#model').html(msg[0].itemcode)
}
});
You can parse json using following code.
var json = $.parseJSON(j);
$(json).each(function(i,val){
$.each(val,function(k,v){
console.log(k+" : "+ v);
});
});
You can do this in PHP as well.
Read this using
$data = file_get_contents('Your_URL')
and then use following function to parse it
$phpArray = json_decode($data);
You can do what ever you want do with that
If you need to output in php, use json_decode()
then get what you want from the loop
$sss= json_decode($data)

Extracting json object and filling in user label

[{"id":1,"name":"ABC"},{"id":2,"name":"XYZ"}]
This is how the data is returned by the controller in json format
I want to store it in the following format:
var json = {
"users": [
{ "id": "1", "name": "ABC" },
{ "id": "2", "name": "XYZ" },
]
}
Following is the code I have used but doesnt work.
var json =
{
"users": $.getJSON("#Url.Action("SearchCMSAdmins")")
}
$("#DistributorCMSAdmin").tokenInput("#Url.Action("SearchWithName")", {
theme: "facebook",
preventDuplicates: true,
prePopulate:json.users
});
Any help is appreciated. Thanks in advance.
$.getJson is asynchronous, so you need to use the success callback for getting the response and doing the operation. Try this INstead.
$.getJSON("#Url.Action("SearchCMSAdmins")",function(data){
var json= {"users":data};
$("#DistributorCMSAdmin").tokenInput("#Url.Action("SearchWithName")", {
theme: "facebook",
preventDuplicates: true,
prePopulate:json.users
});
});

Ember.js / nested json / cannot get data displayed

making first attempt to master Ember.js atm. I'm trying to make a simple CMS, but for some reason I have a problem with getting any data from json displayed. I've already switched from Fixture to RESTAdapter, but I am still stuck with
Error: assertion failed: Your server returned a hash with the key timestamp but you have no mapping for it.
Here's my js code:
App.Store = DS.Store.extend(
{
revision:12,
adapter: 'DS.RESTAdapter'
}
);
App.Menucategory = DS.Model.extend({
timestamp: DS.attr('number'),
status: DS.attr('string')
});
App.MenucategoryRoute = Ember.Route.extend({
model: function() {
return App.Menucategory.find();
}
});
DS.RESTAdapter.reopen({
url: <my url>
});
DS.RESTAdapter.configure("plurals", {
menucategory: "menucategory"
});
Trying to access it with:
<script type="text/x-handlebars" id="menucategory">
{{#each model}}
{{data}}
{{/each}}
</script>
My json structure:
{
"timestamp": 1366106783,
"status": "OK",
"data": [
{
"name": "starters",
"id": 1
},
{
"name": "main dishes",
"id": 2
}]}
Thank you in advance for any help you can provide.
By default the RESTAdapter expects your API to be in a specific format, which your API doesn't conform to, if you can modify your API, you need to get it to return in this format, otherwise you'll need to customize the adapter.
Expected Format (based on your JSON):
{
"menucategory": [
{
"name": "starters",
"id": 1
},
{
"name": "main dishes",
"id": 2
}
],
"meta": {
"timestamp": 1366106783,
"status": "OK",
}
}