I'm trying to format my JSON file so that when parsed it will display as paragraphs with line breaks like this:
This is one line.
This is a second line.
This is a third line.
I've tried \n \\n \p\n but to no avail.
Here is the iPhone screen where I want the text parsed:
Here is the JSON file. Particularly the "body" tag is where I want to do this:
{
"id": 1,
"jsonrpc": "2.0",
"total": 5,
"result": [
{
"id": 1,
"guid": "1d4aa3b2-c059-4fa7-a751-9bca735e4ebb",
"thumb": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1-thumb.jpg",
"picture": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1.jpg",
"title": "Continuing Education 2015 Class Schedule",
"body": "October 24, 2015 from 9am - 1pm\nOctober 24, 2015 from 9am - 1pm",
"tags": ["continuing ed"]
}
]
}
new.service.js file that downloads the JSON:
(function() {
'use strict';
angular
.module('barebone.news')
.factory('newsService', newsService);
newsService.$inject = ['$http', '$q'];
/* #ngInject */
function newsService($http, $q) {
var url = 'https://s3-us-west- 2.amazonaws.com/cisnerostraininggroup/news.json';
var result = [];
var service = {
all: all,
get: get
};
return service;
// *******************************************************
// http://stackoverflow.com/questions/17533888/s3-access-control-allow- origin-header
function all(callback){
$http.get(url)
.success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
result = data.result;
callback(result);
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('ERROR (News):' + status);
callback(result);
});
}
function get(articleId) {
// we take an article from cache but we can request ir from the server
for (var i = 0; i < result.length; i++) {
if (result[i].id === articleId) {
return $q.when(result[i]);
}
}
return $q.when(null);
}
}
})();
Ionic views are html views. So \n are ignored unless in <pre> tags, so after loading your data, iterate over your objects as:
var myobject = {
"id": 1,
"jsonrpc": "2.0",
"total": 5,
"result": [
{
"id": 1,
"guid": "1d4aa3b2-c059-4fa7-a751-9bca735e4ebb",
"thumb": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1-thumb.jpg",
"picture": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1.jpg",
"title": "Continuing Education 2015 Class Schedule",
"body": "October 24, 2015 from 9am - 1pm\nOctober 24, 2015 from 9am - 1pm",
"tags": ["continuing ed"]
}
]
}
myobject.result[0].body = myobject.result[0].body.replace(/(?:\r\n|\r|\n)/g, '<br />');
This will replace all occurrences of line breaks \n with tags and display properly in your html.
The regex to replace newlines with is copied from https://stackoverflow.com/a/784547/2303348
UPDATE:
service to fetch and update results to replace newline with
(function() {
'use strict';
angular
.module('barebone.news')
.factory('newsService', newsService);
newsService.$inject = ['$http', '$q'];
/* #ngInject */
function newsService($http, $q) {
var url = 'https://s3-us-west-2.amazonaws.com/cisnerostraininggroup/news.json';
var result = [];
var nlRegex = new RegExp(/(?:\r\n|\r|\n)/g);
var service = {
all: all,
get: get
};
return service;
// *******************************************************
// https://stackoverflow.com/questions/17533888/s3-access-control-allow- origin-header
function all(callback){
$http.get(url)
.success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
result = data.result;
for (var i in result){
result[i].body = result[i].body.replace(nlRegex, "<br />");
}
callback(result);
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('ERROR (News):' + status);
callback(result);
});
}
function get(articleId) {
// we take an article from cache but we can request ir from the server
for (var i = 0; i < result.length; i++) {
if (result[i].id === articleId) {
return $q.when(result[i]);
}
}
return $q.when(null);
}
}
})();
{
"id": 1,
"jsonrpc": "2.0",
"total": 5,
"result": [
{
"id": 1,
"guid": "1d4aa3b2-c059-4fa7-a751-9bca735e4ebb",
"thumb": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1-thumb.jpg",
"picture": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1.jpg",
"title": "Continuing Education 2015 Class Schedule",
"body": "October 24, 2015 from 9am - 1pm<br />October 24, 2015 from 9am - 1pm",
"tags": ["continuing ed"]
}
]
}
So I figured out how to create line breaks in my JSON. I just created new objects and then added those objects to the corresponding HTML.
JSON file:
{
"id": 1,
"jsonrpc": "2.0",
"total": 5,
"result": [
{
"id": 1,
"guid": "1d4aa3b2-c059-4fa7-a751-9bca735e4ebb",
"thumb": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1-thumb.jpg",
"picture": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1.jpg",
"title": "Continuing Education 2015 Class Schedule",
"body1": "October 24, 2015 from 9am - 1pm",
"body2": "November 7, 2015 from 9am - 1pm",
"body3": "November 18, 2015 from 5:30 - 9:30pm",
"tags": ["continuing ed"]
}
]
}
HTML file (Ionic HTML):
<ion-view view-title="Article">
<ion-content>
<img ng-src="{{vm.article.picture}}" />
<div class="content-inner">
<h1>{{vm.article.title}}</h1>
<p>{{vm.article.body1}}</p>
<p>{{vm.article.body2}}</p>
<p>{{vm.article.body3}}</p>
</div>
</ion-content>
</ion-view>
Related
I use a 'New update timeseries' widget to update a Temperature value.
How can I extract the Temperature value from the Timeseries update message?
f.e. I want to create a new value: msg.temperature2 = msg.temperature + 5
In the rule chain I intercept the update timeseries message with a "Timeseries update
Message:
{
"timeseries": [{
"ts": 1651399784349,
"values": {
"Temperature": 30
}
}]
}
Images:
Rule chain
Transformation script-test script function
It's just a matter of referencing the value correctly. See below for example.
let msg = {
"timeseries": [{
"ts": 1234,
"values":{
"Temperature": 30
}
}]
}
// The code
var newMsg = msg
newMsg.timeseries[0].values.Temperature2 = newMsg.timeseries[0].values.Temperature + 5
// Remove log, uncomment return
console.log({ "msg": newMsg, "metadata": {} })
//return { "msg": msg, "metadata": {} }
I have the following in the application template:
<vaadin-grid id="directory">
<vaadin-grid-tree-column path="name" header="Name"></vaadin-grid-tree-column>
</vaadin-grid>
The iron-ajax calls the following on a successful response:
getlist(request) {
var myResponse = request.detail.response;
console.log(myResponse);
this.$.directory.items = myResponse;
}
The data that is returned is:
[
{
"name": "apps",
"fullpath": "/db/system/xqdoc/apps",
"children": [
{
"name": "xqDoc",
"fullpath": "/db/system/xqdoc/apps/xqDoc",
"children": [
{
"name": "modules",
"fullpath": "/db/system/xqdoc/apps/xqDoc/modules",
"children": [
{
"name": "config.xqm.xml",
"fullpath": "/db/system/xqdoc/apps/xqDoc/modules/config.xqm.xml"
},
{
"name": "xqdoc-lib.xqy.xml",
"fullpath": "/db/system/xqdoc/apps/xqDoc/modules/xqdoc-lib.xqy.xml"
}
]
}
]
}
]
}
]
The apps shows up, but when I expand the apps node, then xqDoc node doees not show up.
Do I need additional data in the dataset?
Am I missing some coding that is needed?
I have the solution.
<vaadin-grid id="directory" selected-items="{{selected}}">
<vaadin-grid-tree-column path="name" header="Name"item-has-children-path="hasChildren"></vaadin-grid-tree-column>
</vaadin-grid>
I setup the provider using the connectedCallback and not to use an iron-ajax for talking with the server.
connectedCallback() {
super.connectedCallback();
const grid = this.$.directory;
this.$.directory.dataProvider = function(params, callback) {
let url = "/exist/restxq/xqdoc/level" +
'?page=' + params.page + // the requested page index
'&per_page=' + params.pageSize; // number of items on the page
if (params.parentItem) {
url += '&path=' + params.parentItem.fullpath;
}
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var response = JSON.parse(xhr.responseText);
callback(
response.data, // requested page of items
response.totalSize // total number of items
);
};
xhr.open('GET', url, true);
xhr.send();
};
this.$.directory.addEventListener('active-item-changed', function(event) {
const item = event.detail.value;
if (item && item.hasChildren == false) {
grid.selectedItems = [item];
} else {
grid.selectedItems = [];
}
});
}
The web service returns a level of the tree:
{
"totalSize": 2,
"data": [
{
"name": "apps",
"fullpath": "/apps",
"hasChildren": true
},
{
"name": "lib",
"fullpath": "/lib",
"hasChildren": true
}
]
}
The codebase is here: https://github.com/lcahlander/xqDoc-eXist-db
I am unable to insert multiple rows in database using Post method in MVC web API. I have written code for it but when i am testing by inserting multiple rows through postman it is giving error. At line first the variable "delegatetable" shows null due to which error is coming. i am not doing database connection through entity framework, i have created a DelegateTable class.
public HttpResponseMessage Post(List<DelegateTable> delegatetable)
{
try
{
using (var delegateContext = new ShowContext())
{
foreach (DelegateTable item in delegatetable)
{
DelegateTable delegates = new DelegateTable();
delegates.Salutation__c = item.Salutation__c;
delegates.First_Name__c = item.First_Name__c;
delegates.Last_Name__c = item.Last_Name__c;
delegates.Account_Name__c = item.Account_Name__c;
delegates.Contact_Email__c = item.Contact_Email__c;
delegates.Category__c = item.Category__c;
delegates.Conference_Type__c = item.Conference_Type__c;
delegates.Conference_Selection__c = item.Conference_Selection__c;
delegates.Payment_Statuss__c = item.Payment_Statuss__c;
delegates.Barcode__c = item.Barcode__c;
delegateContext.SaveChanges();
}
var message = Request.CreateResponse(HttpStatusCode.Created, delegatetable);
message.Headers.Location = new Uri(Request.RequestUri.ToString());
return message;
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
Json data that i am passing is below
[
{
"attributes": {
"type": "Registration__c",
"url": "/services/data/v43.0/sobjects/Registration__c/a3h8E0000009VuVQAU"
},
"Salutation__c": "Dr.",
"First_Name__c": "Test",
"Last_Name__c": "Test",
"Account_Name__c": "Test",
"Contact_Email__c": "test123#gmail.com",
"Category__c": "Test",
"Conference_Type__c": null,
"Conference_Selection__c": null,
"Payment_Statuss__c": null,
"Barcode__c": "Test"
},
{
"attributes": {
"type": "Registration__c",
"url": "/services/data/v43.0/sobjects/Registration__c/a3hD0000001kEfOIAU"
},
"Salutation__c": "Mr.",
"First_Name__c": "Demo",
"Last_Name__c": "Demo",
"Account_Name__c": "Demo",
"Contact_Email__c": "Demo#gmail.com",
"Category__c": "Demo",
"Conference_Type__c": null,
"Conference_Selection__c": null,
"Payment_Statuss__c": null,
"Barcode__c": null
}
]
You may try to reformat your payload as a JSON array, as the problem might be that the payload cannot be converted to a List.
Try this:
{
"delegates" :
[
{
"attributes": ..., ...
},
{ "attributes": ..., ...
},
...
]
}
Is it possible to send few objects into gson rendering view?
I tried to use in controller:
respond trainings, [status: OK, view:"trainingsByClients", model: [myVariable: "test", anotherVariable: 123]]
and in gson view:
model {
Iterable<Training> trainingList
String myVariable
}
json {
myVariable myVariable
trainings tmpl.training(trainingList ?: [])
}
and it responds with:
{
"myVariable": null,
"trainings": [
{
"id": 3,
"name": "test t",
"numberOfAbsentClients": 0,
"startDate": "2016-11-20T09:00:00+0000",
"numberOfClients": 2,
"section": {
"id": 1,
"name": "test sec"
}
},
{
"id": 10,
"name": "test 2",
"numberOfAbsentClients": 0,
"startDate": "2016-11-09T11:00:00+0000",
"numberOfClients": 2,
"section": {
"id": 2,
"name": "sec 2"
}
}
]
}
ok, I found solution:
render(view: "trainingsByClients", model: [trainingList: trainings, myVariable: "asdg"])
so we should use render instead of respond. Respond is not adding properly additional model objects.
Actually you have to use the g.render method for all model properties when using respond
example gson view:
model {
Iterable<Training> trainingList
String myVariable
}
json {
myVariable g.render(myVariable)
trainings tmpl.training(trainingList ?: [])
}
This is only related to the models you parse in the respond.model parameter
When performing a search request to the API, the height & width fields of the thumbnails are not included in the response, even when specified in the fields parameter. Here's an example for a JS object that provides the API parameters:
{
part: 'snippet',
type: 'video',
order: 'relevance',
q: 'test',
fields: 'items(snippet(thumbnails(high(url,height,width))))'
}
Which translates into the following request URL:
https://www.googleapis.com/youtube/v3/search?order=relevance&part=snippet&q=test&fields=items(snippet(thumbnails(high(url%2Cheight%2Cwidth))))&type=video&key={YOUR_API_KEY}
This call yields the following response without the width or height of the thumbnails.
{
"items": [
{
"snippet": {
"thumbnails": {
"high": {
"url": "https://i.ytimg.com/vi/3HKs8WTGzw8/hqdefault.jpg"
}
}
}
},
{
"snippet": {
"thumbnails": {
"high": {
"url": "https://i.ytimg.com/vi/vW_8K_mLtsU/hqdefault.jpg"
}
}
}
},
{
"snippet": {
"thumbnails": {
"high": {
"url": "https://i.ytimg.com/vi/4Yk-jd4BHys/hqdefault.jpg"
}
}
}
},
{
"snippet": {
"thumbnails": {
"high": {
"url": "https://i.ytimg.com/vi/HU9mnag7vSM/hqdefault.jpg"
}
}
}
},
{
"snippet": {
"thumbnails": {
"high": {
"url": "https://i.ytimg.com/vi/pyrH7b0zHwU/hqdefault.jpg"
}
}
}
}
]
}
This similarly does not work for the default or medium thumbnail keys either.
How can these fields be retrieved?
The search endpoint won't return those details. You'll have to take the IDs returned from the search and do another API call to the videos endpoint for the snippet. For instance
https://www.googleapis.com/youtube/v3/videos?part=snippet&id={VIDEO_ID}&key={YOUR_API_KEY}
As per Youtube Date API (v3) Search Method returns as per document but its not working properly.
Now you should try alternative method by the API call of Video.
URL :- https://www.googleapis.com/youtube/v3/videos?part=snippet&id={VIDEO_ID}&key={YOUR_API_KEY}
VIDEO_ID = Return by Search API
YOUR_API_KEY = Google Project API key
Try it
$.get(
"https://www.googleapis.com/youtube/v3/search",{
order:'relevance',
part : 'snippet',
type : 'video',
q: 'test',
key: 'XXXXXXX'},
function(data) {
alert(data.items.length);
$.each( data.items, function( i, item ) {
pid = item.id.videoId;
getVids(pid);
});
}
);
//Get Videos
function getVids(pid){
$.get(
"https://www.googleapis.com/youtube/v3/videos",{
part : 'snippet',
id : pid,
key: 'XXXXXXXX'},
function(data) {
//Code whatever you want
}
);
}