How to format a json string and show result in table? - json

I am using CakePhp _serialize method to make a web service and show the data in JSON format.I used the .json extension at the end of the URL to show this data.Now i want to show this data in table.Output image is attached.Is this possible then how i can do it?
Thanks

The format is a bit odd. I would prefer something like: "task_list": [ .... ]; iterating over objects is always a bit tedious.
Here is the jQuery code:
var data = ...;
var items = data["task_list"];
var table = $('<table/>');
table.appendTo($('body'));
$.each(items, function(id, value) {
var tr = $('<tr/>');
table.append(tr);
$('<td/>').text(id).appendTo(tr);
$('<td/>').text(value).appendTo(tr);
});

Related

How to extract image link from JSON in Flutter - Regex

I am a beginner in Flutter. I was making a note-taking app, which contains flutter_quill. When I save the data into JSON, I got this data.
[{"insert":"Ttttttt\n"},{"insert":{"image":"/data/user/0/com.example.notes/app_flutter/image_picker49200670200148407.jpg"}},{"insert":"\n\n"}]
I just need the .jpg file data part - /data/user/0/com.example.notes/app_flutter/image_picker49200670200148407.jpg, How to get that? Using Regex?
Something like this?
void main() {
var l = <Map<String, dynamic>>[
{"insert": "Ttttttt\n"},
{"insert": { "image":"/data/user/0/com.example.notes/app_flutter/image_picker49200670200148407.jpg"}
},
{"insert": "\n\n"}
];
var imageElement = l.firstWhere((element) =>
element["insert"] is Map && element["insert"].containsKey("image"));
var image;
if (imageElement!=null) image=imageElement["insert"]["image"];
print (image);
}
loop all insert then check, have key "image" so get that value

how to fix undefined data from json data?

I have JSON data like this {"id":"27","kode":"1111","judul":"q","penulis":"q","penerbit":"q","tahun_terbit":"1","tgl_masuk":"2017-08-09","tgl_update":"2017-08-09"}
How to display id? I try with data[0].id, data['id'] or data.id and the result remains undefined. Thanks for the answers
You have to parse your string JSON into an Object
var str = '{"id":"27","kode":"1111","judul":"q","penulis":"q","penerbit":"q","tahun_terbit":"1","tgl_masuk":"2017-08-09","tgl_update":"2017-08-09"}';
var obj = jQuery.parseJSON( str );
then you can :
console.log(obj.id);
If the language is javascript, here we go:
var data = {"id":"27","kode":"1111","judul":"q","penulis":"q","penerbit":"q","tahun_terbit":"1","tgl_masuk":"2017-08-09","tgl_update":"2017-08-09"};
console.log(data.id);
Assuming this is JavaScript, and since you've referred to "data" in your question, your statement might look like this:
var data = {"id":"27","kode":"1111","judul":"q","penulis":"q","penerbit":"q","tahun_terbit":"1","tgl_masuk":"2017-08-09","tgl_update":"2017-08-09"}
If so, you can access the "id" property like this
alert(data.id);

How to get the values from the Json using Jquery

Hi I'm new to Jquery and i don't know how to use json data. I have a Json which is coming as a response. My success function is
success: function(resp)
{
var x = JSON.stringify(resp);
alert(x);
}
alert is having following JSON
{"xyz":{"abc":[{"action":"fail","result":"xxx"},{"action":"pass","resut":"yyy"}]}}
I want action value alone. How can i get this value from x variable and how can i utilize this value in HTML. Thanks in advance.
When you use JSON.stringify() you are turning it into a string, you want it as an object to access the data. Try this:
success: function(resp) {
alert(resp.xyz.abc[0].action);
var x = resp.xyz.abc[0].action // to use the value in html later on
}
If it is returned as a string (I can't tell at this point), you can turn it into an object (as long as it is valid JSON) by using $.parseJSON()
success: function(resp)
{
var x = $.parseJSON(resp);
var xyz = x.xyz;
var pass = x.xyz.abc[1].action;
}
or you can loop though each of the array by $.each
success: function(resp)
{
var x = $.parseJSON(resp);
$.each(x.xyz.abc, function(index, element){
alert('action:' + element.action + ', result:' + element.resut)
});
}
i think, and don't take it personally,that your JSON object is not well organized as an object to get and to have. Action,from my perspective is either fail or success, and reading it, as you saw in the above good answer, will give you exactly what you want.
What's the point in getting a JSON with data structured like you did, with 2 possible answers encoded in it, when there is only one available (either success or fail).

How to get the name of the token

I need to parse json files which look as follows:
{"20120101":{"Jeff":{"Status":"Sleepy", "Weight":212}, "Cathy":{"Status":"Angry", "Weight":172}}
{"20120102":{"Jeff":{"Status":"Alert", "Weight":207}, "Cathy":{"Status":"Sick", "Weight":168}}
I cannot figure out a way to extract the dates (20120101 and 20120102) and names (Jeff and Cathy) from my json. My attempts look as follows:
private void LoadFile(string fileName)
{
var json = File.ReadAllText(fileName);
JObject days = JObject.Parse(json);
foreach (var dayAsObject in days)
{
var day = (JToken) dayAsObject;
var a = day.Root.ToString();
var t = day.ToString();
var z = day.First;
Console.WriteLine(day+t+z+a);
}
Better formulated json would look like
{"20120101":{ "name":"Jeff", "Status":"Sleepy", "Weight":212}, { "name":"Cathy", "Status":"Angry", "Weight":172}}
Then it is very easy to get day["name"]. I would suggest modifying your json. If you absolutely cant, I think the property you're looking for is PropertyName.
I tend to use the built in System.Web.Script.Serialization JSON libary. If you don't need to do fancy stuff it works great with the dynamic object type.

Convert Json response to HTML Table

How do I display all the points in this Json return statement to an HTML table on my view?
return Json(
new
{
vol = exposure.Volatility,
points =
from point in exposure.ExposurePointCollection
select new
{
date = point.ExposureDate,
val = point.MaximumExposure
}
});
If this is the result of an ajax call, you will need to iterate over the collection (presumably the "points" value) and add them to the DOM via javascript. jQuery can help with this, but there are also plug-ins for jQuery that will do virtually all of the work for you, something like jqGrid comes to mind.
Looping through the json result (assuming "points" is what you are looping over)
success: function(data){
jquery.each(data.points, function(index, value){
//index is the index of the array, value is the actual value
});
}
Look here for using jQuery to modify the DOM: http://api.jquery.com/html/#html2