0

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:

enter image description here

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);
       }
     }   
  })();
Paul
  • 1,179
  • 3
  • 14
  • 38

3 Answers3

1

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);
       }
     }   
  })();
Community
  • 1
  • 1
mani
  • 3,068
  • 2
  • 14
  • 25
  • Thanks for your answer, but your JSON isn't valid on JSlint.com – Paul Nov 18 '15 at 22:29
  • I just copied the json from your question... JSONLint doesnt allow extra commas in array objects, but this answer will still parse. The idea is that you iterate over your results, and using the regex, you replace every occurence of a new line character with a
    tag... - also i will update my answer to satisfy your jsonlint condition...
    – mani Nov 19 '15 at 00:47
  • Well as you probably know, JSON isn't very forgiving when it comes to syntax, and your JSON isn't passing JSLINT and is not parsing in the app. – Paul Nov 19 '15 at 01:06
  • i did update the answer with json that passes. the point is: replace all occurrences of new characters with
    tags because ionic views are html views.
    – mani Nov 19 '15 at 01:09
  • FYI: the json in your question isn't valid on JSlint.com – mani Nov 19 '15 at 01:11
  • I just updated my JSON. It passes now. Yours doesn't though. I understand what you are saying as the solution, but the JSON isn't valid, so I can't mark your answer as correct. – Paul Nov 19 '15 at 01:19
  • seriously dude, i already updated the json that passes. are you even reading the comments? see here: http://tinypic.com/view.php?pic=23s81lk&s=9 – mani Nov 19 '15 at 01:24
  • Yes, I've read your comments, but the JSON doesn't pass so the answer isn't valid. – Paul Nov 19 '15 at 01:41
  • because the code in my answer is javascript! if you're using ionic, you must be using javascript. var myobject = {} initialises a JAVASCRIPT object... and the last line of code is replacing the \n with
    using JAVASCRIPT
    – mani Nov 19 '15 at 01:43
  • Yes ionic uses Javascript, but I'm placing this JSON file on my server. I'm not running Javascript on the server. I have a news.json file that I download from my JS file. I'm not looking for Javascript answer. It needs to be in pure JSON. My question is "Format JSON for a line break when parsed on iPhone" – Paul Nov 19 '15 at 01:50
  • you will put this js wherever you download news.json, and iterate over your results. but hey, see my other answer with your updated json... and good luck with ionic – mani Nov 19 '15 at 01:58
  • And where do I put the JS? I updated my answer with my news.service.js file that downloads the JSON file from the server. – Paul Nov 19 '15 at 02:04
  • Unfortunately I updated the new.service.js file with your changes, but I get an infinite loading screen when downloading the new.json file – Paul Nov 19 '15 at 04:27
  • just realised there was a space in your s3 url. can you run it again and paste the console output? – mani Nov 19 '15 at 12:41
  • Yeah, sorry, I updated the code, but I still get the
    symbol in the parsed output. No line break. My solution, although maybe a little cumbersome, did work.
    – Paul Nov 19 '15 at 18:07
0
{
        "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"]
          }
        ]
      }
mani
  • 3,068
  • 2
  • 14
  • 25
  • I tried this already. This is what it looks like in the phone when I put this on my server: https://www.dropbox.com/s/v1h3zi3d9pij4p1/JSON_%3Cbr%3E.jpg?dl=0 – Paul Nov 19 '15 at 02:09
0

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>
Paul
  • 1,179
  • 3
  • 14
  • 38