-5

I have a variable named "text" in javascript and it contains following value

text={"text":"@RT #Olle_Carly Nuevas filtraciones del iPhone 6: así sería comparado con el Samsung Galaxy S5 y el iPhone 5S: Des... http://t.co/eRuXLS6N3M"};

I parsed it using

JSON.parse(text);

But it gives the following error

Unexpected token

How to solve the error.. Thanks in advance.....

Reddevil
  • 682
  • 1
  • 9
  • 22
  • `"text":` not `text=` – mplungjan Jun 25 '14 at 04:56
  • 2
    You sure this is JSON? The text variable contains invalid JSON. JSON structure is something given here : http://en.wikipedia.org/wiki/JSON – Rahul Patil Jun 25 '14 at 04:57
  • You're trying to use the JSON parser on something that is clearly not JSON. – Robby Cornelissen Jun 25 '14 at 04:58
  • sorry...Now i edited it – Reddevil Jun 25 '14 at 05:04
  • That is not even JSON. – Derek 朕會功夫 Jun 25 '14 at 05:04
  • 2
    `text` holds an array object, not JSON. Just don't use `JSON.parse` to solve the problem. – Felix Kling Jun 25 '14 at 05:06
  • @FelixKling...thanks for ur reply...how to parse that json array – Reddevil Jun 25 '14 at 05:07
  • 2
    @Reddevil: Again, it's not a "JSON array" its an object literal. You don't have to and cannot parse it because the JavaScript engine does that for you. `text` already is an **object**, just access it. If you don't know how: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties – Felix Kling Jun 25 '14 at 05:08
  • @FelixKling:ok...but I have more text values as below text=[ {"text":"#Olle_Carly Nuevas filtraciones del iPhone 6: así sería comparado con el Samsung Galaxy S5 y el iPhone 5S: Des... http://t.co/eRuXLS6N3M"}, {"text":"#ROBIN_ALVAREZ01 Nuevas filtraciones del iPhone 6: así sería comparado con el Samsung Galaxy S5 y el iPhone 5S... http://t.co/AZmKXwg9wZ"}, {"text":"(#Dmjeferson) Nuevas filtraciones del iPhone 6: así sería comparado con el Samsung Galaxy S5 y e... http://t.co/7tpLuvhFSF (@Dmjeferson)"}, ]...then how to parse it or get second or third text values – Reddevil Jun 25 '14 at 05:12
  • Again that seems to be an array holding objects. Just access the array and its elements. Any good JS tutorial explains how to do this, e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Predefined_Core_Objects#Referring_to_Array_Elements, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties – Felix Kling Jun 25 '14 at 05:19
  • Actually, per the latest JSON spec, this would be valid JSON: `json = '"string"';` The problem with the original (pre-edit) JSON sample was that the string element wasn't quoted. Literals are permitted to be valid JSON now. But quoting rules still apply. – DavidO Jun 25 '14 at 05:30

1 Answers1

0

You are using a javascript variable that is not compatible with JSON script. The code should be in the format of var text = '{parameters and text}' then var obj = JSON.parse(text); then manipulate obj how you like

DonJunior
  • 43
  • 1
  • 8