-3

I am trying to convert String to Javascript array by using following functions but none of them seems working for me . Functions that I have used for conversion are as follows :-

  1. var array = angular.fromJson(String);
  2. var array = JSON.parse(String);

Can any body tell me how to do conversion of string to JavascriptArray in angularjs.

Here is my String :-

"[
      [new Date("2015/08/04 16:33:05"), 26.2, 15.0, 36.0],
      [new Date("2015/08/04 16:38:05"), 26.0, 15.0, 36.0],
      [new Date("2015/08/04 16:43:05"), 26.2, 15.0, 36.0],
      [new Date("2015/08/04 16:48:05"), 26.1, 15.0, 36.0],
      [new Date("2015/08/04 16:53:05"),
        26.2, 15.0, 36.0
      ],
      [new Date("2015/08/04 16:58:05"), 26.2, 15.0, 36.0],
      [new Date("2015/08/04 17:03:05"), 26.3, 15.0, 36.0],
      [new Date("2015/08/04 17:08:05"), 26.3, 15.0, 36.0],
    ]"

Thanks

demo demo
  • 69
  • 8

3 Answers3

0

Try:

var obj = JSON.parse(String);
var array = obj["value"];
cesarluis
  • 897
  • 6
  • 14
  • Hey Cesarluis can you please tell me how to extract javascript array from this strng value :- "value":"[[ new Date(\"2015/08/06 16:37:05\"), 28.8, 15.0, 36.0],[ new Date(\"2015/08/06 16:42:05\"), 28.8, 15.0, 36.0],]" – demo demo Aug 07 '15 at 08:29
  • This is not a valid JSON string. If strval equals what you said, wrap it in "{" and "}" (e.g. objstrval = "{" + strval + "}") and then use what @cesarlius says - JSON.parse(objstrval) – Igor R Aug 07 '15 at 08:32
  • you might want to read about what is valid JSON, e. g. here http://www.fizerkhan.com/blog/posts/JSON-is-not-Javascript-Object.html – Igor R Aug 07 '15 at 08:34
  • Thanks for your replies but still its not working for me ..i tried it by two different ways one is :- var ObjStrValue = "{" + graphData + "}" var array =angular.fromJson(ObjStrValue); alert(array); And another as mentioned by @igor R i.e var ObjStrValue = "{" + graphData + "}" var array =JSON.parse(ObjStrValue); alert(array); – demo demo Aug 07 '15 at 08:44
  • new Date is not a valid JSON, @Ashwin K below is right in both things - eval would help but is bad practice. See a better solution through Function constructor here http://dfkaye.github.io/2014/03/14/javascript-eval-and-function-constructor/ – Igor R Aug 07 '15 at 08:52
0

Since it is not a JSON string, using eval(string) method you may get desired output.

But people say eval is evil : Why?

Demo: Fiddler

Updated: Demo

Community
  • 1
  • 1
Ashwin K
  • 210
  • 1
  • 6
  • Using eval(String) , I am not getting desire result. Here is my full string which I am getting in Api response: http://piratepad.net/ep/pad/view/ro.p2NNzWX8$g$/latest and is jsfiddle : http://jsfiddle.net/9hWQ6/14/ – demo demo Aug 07 '15 at 09:28
0

Your array is in string format with unevaluated date expression. just evaluate it as below and you get your array.

 var array = eval("[ [new Date('2015/08/04 16:33:05'), 26.2, 15.0, 36.0],  [new Date('2015/08/04 16:43:05'), 26.2, 15.0, 36.0] ]");
  • Please check my new thread : http://stackoverflow.com/questions/31912460/ng-init-in-ng-repeat-shows-only-the-last-item-in-dygraph – demo demo Aug 10 '15 at 05:56