Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30165

Re: convert a string into a json object,

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: convert a string into a json object,
Date 2016-04-01 10:42 +0200
Organization albasani.net
Message-ID <ndlc9h$9tf$1@news.albasani.net> (permalink)
References <c8134afb-b7f5-4e2d-b3e6-90f8a3594544@googlegroups.com>

Show all headers | View raw


JRough wrote:
> var string='Janis_SF_WebDev"\n"Debby_SJ_WebDev"\n"Stephanie_Fl_Recruite"\n"';

The line delimiter in this string is '"\n"' instead of '\n' - is that
intentional?

> function doJson(string){
> var arr = string.split("\n");

split() should use the same delimiter as the input: '"\n"'.

>    for (i=0;i< 2;i++){

The variable i needs to be declared. And the code is more flexible if
the number of records isn't hard-coded. Speaking of which, the string
contains three records, but you're only counting: zero, one.

>         var  str=  arr[i].split("_");
>             var jsonObj= {};
>                 jsonObj.name =str[0];
>                 jsonObj.city= str[1];
>                 jsonObj.job = str[2];

This is ok, but can be writen in a more compact way:

    var jsonObj = {
        name: str[0],
        city: str[1],
        job:  str[2],
    };

>     }
>     return jsonObj;

This will return the last assembled object. You probably want a list of
all those objects.

>  }
> 
> var myObj = doJson(string);


You seem to have a misunderstanding about what JSON is: it's a simple
string format for data serialization. What you're building here are
JavaScript objects, not JSON strings.


Here is a version which avoids the mentioned problems:

  // I'll assume the line delimiter is actually \n
  var string='Janis_SF_WebDev\nDebby_SJ_WebDev\nStephanie_Fl_Recruite\n';

  function parseString (string)
  {
      var lines = string.split("\n");
      var result = [];  // this array is new, it holds all your objects

      for (var i = 0; i < lines.length; i++) {
          if (!lines[i].length) {
              continue; // skips empty records, like the last one
          }
          var str = lines[i].split("_");
          var obj= {
              name: str[0],
              city: str[1],
              job:  str[2],
          };
          result.push(obj);  // add the object to the result
      }

      return result;  // returns the array instead of a single object
  }

  // this converts the input string into an array of objects
  var myList = parseString(string);

  // this produces a JSON string from what that list
  var json = JSON.stringify(myList);


- stefan

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

convert a string into a json object, JRough <janis.rough@gmail.com> - 2016-03-31 19:12 -0700
  Re: convert a string into a json object, Stefan Weiss <krewecherl@gmail.com> - 2016-04-01 10:42 +0200
    Re: convert a string into a json object, JRough <janis.rough@gmail.com> - 2016-04-01 17:32 -0700
  Re: convert a string into a json object, Stefan Weiss <krewecherl@gmail.com> - 2016-04-01 10:49 +0200
    Re: convert a string into a json object, John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-01 19:21 +0100
      Re: convert a string into a json object, Aleksandro <aleksandro@gmx.com> - 2016-04-01 16:21 -0300
      Re: convert a string into a json object, Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-01 20:36 +0100
        Re: convert a string into a json object, Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 04:07 +0200
          Re: convert a string into a json object, Aleksandro <aleksandro@gmx.com> - 2016-04-02 12:07 -0300
            Re: convert a string into a json object, Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 17:36 +0200
              Re: convert a string into a json object, Aleksandro <aleksandro@gmx.com> - 2016-04-02 21:01 -0300
          Re: convert a string into a json object, Scott Sauyet <scott@sauyet.com> - 2016-04-02 20:40 +0000
    Re: convert a string into a json object, Stefan Weiss <krewecherl@gmail.com> - 2016-04-02 01:18 +0200
      Re: convert a string into a json object, John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-02 11:29 +0100
    Re: convert a string into a json object, Scott Sauyet <scott@sauyet.com> - 2016-04-02 20:30 +0000
  Re: convert a string into a json object, JRough <janis.rough@gmail.com> - 2016-04-01 11:32 -0700
    Re: convert a string into a json object, Stefan Weiss <krewecherl@gmail.com> - 2016-04-02 00:51 +0200
      Re: convert a string into a json object, Aleksandro <aleksandro@gmx.com> - 2016-04-02 12:03 -0300
  Re: convert a string into a json object, Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 03:48 +0200
  Re: convert a string into a json object, Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 04:25 +0200

csiph-web