0

I Search but i cant find any solution. i get this string from server :

 [
  {
    'id': 0,
    'name': 'Adventure Works',
    'uniqueName': '[AdventureWorksDW2012Multidimensional-EE].[Adventure Works]'
  },
  {
    'id': 1,
    'name': 'Channel Sales',
    'uniqueName': '[AdventureWorksDW2012Multidimensional-EE].[Channel Sales]'
  },
  {
    'id': 2,
    'name': 'Direct Sales',
    'uniqueName': '[AdventureWorksDW2012Multidimensional-EE].[Direct Sales]'
  },
  {
    'id': 3,
    'name': 'Finance',
    'uniqueName': '[AdventureWorksDW2012Multidimensional-EE].[Finance]'
  },
  {
    'id': 4,
    'name': 'Mined Customers',
    'uniqueName': '[AdventureWorksDW2012Multidimensional-EE].[Mined Customers]'
  },
  {
    'id': 5,
    'name': 'Sales Summary',
    'uniqueName': '[AdventureWorksDW2012Multidimensional-EE].[Sales Summary]'
  },
  {
    'id': 6,
    'name': 'Sales Targets',
    'uniqueName': '[AdventureWorksDW2012Multidimensional-EE].[Sales Targets]'
  }
]

But it is a string and i want it as a JavaScript Object. If i remove first and last" from string by hand every thing is ok but how to convert this string to JavaScript Object ?

Update

If i use jquery for parsing like this :

var data = $.parseJSON( response.d );

I get this error :

Uncaught TypeError: Cannot use 'in' operator to search for '727' in {'id':0,'name':'Adventure Works','uniqueName':'[AdventureWorksDW2012Multidimensional-EE].[Adventure Works]'},{'id':1,'name':'Channel Sales','uniqueName':'[AdventureWorksDW2012Multi...<omitted>...'} 

** Edit **

This is JSFIDDLE link.

sedran
  • 3,498
  • 3
  • 23
  • 39
MBehtemam
  • 7,865
  • 15
  • 66
  • 108
  • 1
    [`JSON.parse(yourString);`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)? – James Allardice Feb 27 '14 at 08:54
  • After your update, you are not showing the relevant code. There is no `in` operator in the given codesample. – Sirko Feb 27 '14 at 09:37

3 Answers3

1

Use JSON.parse("[{...}]"); to convert it into Javascript object.

sedran
  • 3,498
  • 3
  • 23
  • 39
  • I try your way [here](http://jsbin.com/disegixa/1/edit?js,console) in jsbin but i get an error. `Unexcepted token '` – MBehtemam Feb 27 '14 at 09:09
  • Yes. this is a [fiddle](http://jsfiddle.net/MBehtemam/89pRk/) that i cant parse and iterate it.thanks – MBehtemam Mar 01 '14 at 05:13
  • 1
    I realized the problem. Your JSON string is invalid. You should use double quotes in json, not single. See this: http://jsfiddle.net/89pRk/1/ – sedran Mar 01 '14 at 08:28
1

you can use JSON.parse() method for your JSON String to Javascript Object.

Zeeshan
  • 1,659
  • 13
  • 17
Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80
0

You can use:

JSON.parse("some json stringified object")

To create string from json object use:

JSON.stringify(OBJECT);
Farkhat Mikhalko
  • 3,565
  • 3
  • 23
  • 37