Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
I trying to display top ten trending twitter topics in my webpage.I have used twitter get/trends API with php. I used ajax call to php on my homepage and it returns json_encoded result in json format.
The json format looks like this
[ { "trends": [ { "name": "#MukhangPeraAngPilipinas", "query": "%23MukhangPeraAngPilipinas", "url": "http:\/\/twitter.com\/search?q=%23MukhangPeraAngPilipinas", "promoted_content": null }, { "name": "#FelizDiaDelPadre", "query": "%23FelizDiaDelPadre", "url": "http:\/\/twitter.com\/search?q=%23FelizDiaDelPadre", "promoted_content": null }, { "name": "#BeforeTwitterI", "query": "%23BeforeTwitterI", "url": "http:\/\/twitter.com\/search?q=%23BeforeTwitterI", "promoted_content": null }, { "name": "#BDMELODI_164", "query": "%23BDMELODI_164", "url": "http:\/\/twitter.com\/search?q=%23BDMELODI_164", "promoted_content": null }, { "name": "#\u00d6nSe\u00e7imeGirenTekLider", "query": "%23%C3%96nSe%C3%A7imeGirenTekLider", "url": "http:\/\/twitter.com\/search?q=%23%C3%96nSe%C3%A7imeGirenTekLider", "promoted_content": null }, { "name": "T\u00fanez", "query": "T%C3%BAnez", "url": "http:\/\/twitter.com\/search?q=T%C3%BAnez", "promoted_content": null }, { "name": "Nasir Hossain", "query": "%22Nasir+Hossain%22", "url": "http:\/\/twitter.com\/search?q=%22Nasir+Hossain%22", "promoted_content": null }, { "name": "Dwight Walker", "query": "%22Dwight+Walker%22", "url": "http:\/\/twitter.com\/search?q=%22Dwight+Walker%22", "promoted_content": null }, { "name": "\u063a\u064a\u0631 \u0627\u0644\u0645\u0634\u0631\u0648\u0639", "query": "%22%D8%BA%D9%8A%D8%B1+%D8%A7%D9%84%D9%85%D8%B4%D8%B1%D9%88%D8%B9%22", "url": "http:\/\/twitter.com\/search?q=%22%D8%BA%D9%8A%D8%B1+%D8%A7%D9%84%D9%85%D8%B4%D8%B1%D9%88%D8%B9%22", "promoted_content": null }, { "name": "\u0635\u0648\u062a \u0627\u0644\u0645\u0637\u0631", "query": "%22%D8%B5%D9%88%D8%AA+%D8%A7%D9%84%D9%85%D8%B7%D8%B1%22", "url": "http:\/\/twitter.com\/search?q=%22%D8%B5%D9%88%D8%AA+%D8%A7%D9%84%D9%85%D8%B7%D8%B1%22", "promoted_content": null } ], "as_of": "2015-03-19T11:19:49Z", "created_at": "2015-03-19T11:15:05Z", "locations": [ { "name": "Worldwide", "woeid": 1 } ] } ]


So when i try to access the json object like response.trends.name.....it shows an error saying "cannot find property name of undefined".....
Can someone help me with this so i can complete my application
Posted
Comments
Tushar sangani 19-Mar-15 8:11am    
use the json data using the Json.parse

1 solution

Have a closer look at the returned JSON.

I've re-formatted it here in an effort to make its structure more clear. (copy the contents of this code-block and paste it into an editor that doesn't wrap long lines to make it clear. CP's line-wrapping muddies the waters a bit)

JavaScript
[ 
	{ 
		"trends": 
					[ 
						{ "name": "#MukhangPeraAngPilipinas", "query": "%23MukhangPeraAngPilipinas", "url": "http:\/\/twitter.com\/search?q=%23MukhangPeraAngPilipinas", "promoted_content": null }, 
						{ "name": "#FelizDiaDelPadre", "query": "%23FelizDiaDelPadre", "url": "http:\/\/twitter.com\/search?q=%23FelizDiaDelPadre", "promoted_content": null },
						{ "name": "#BeforeTwitterI", "query": "%23BeforeTwitterI", "url": "http:\/\/twitter.com\/search?q=%23BeforeTwitterI", "promoted_content": null }, 
						{ "name": "#BDMELODI_164", "query": "%23BDMELODI_164", "url": "http:\/\/twitter.com\/search?q=%23BDMELODI_164", "promoted_content": null }, 
						{ "name": "#\u00d6nSe\u00e7imeGirenTekLider", "query": "%23%C3%96nSe%C3%A7imeGirenTekLider", "url": "http:\/\/twitter.com\/search?q=%23%C3%96nSe%C3%A7imeGirenTekLider", "promoted_content": null },
						{ "name": "T\u00fanez", "query": "T%C3%BAnez", "url": "http:\/\/twitter.com\/search?q=T%C3%BAnez", "promoted_content": null }, 
						{ "name": "Nasir Hossain", "query": "%22Nasir+Hossain%22", "url": "http:\/\/twitter.com\/search?q=%22Nasir+Hossain%22", "promoted_content": null }, 
						{ "name": "Dwight Walker", "query": "%22Dwight+Walker%22", "url": "http:\/\/twitter.com\/search?q=%22Dwight+Walker%22", "promoted_content": null }, 
						{ "name": "\u063a\u064a\u0631 \u0627\u0644\u0645\u0634\u0631\u0648\u0639", "query": "%22%D8%BA%D9%8A%D8%B1+%D8%A7%D9%84%D9%85%D8%B4%D8%B1%D9%88%D8%B9%22", "url": "http:\/\/twitter.com\/search?q=%22%D8%BA%D9%8A%D8%B1+%D8%A7%D9%84%D9%85%D8%B4%D8%B1%D9%88%D8%B9%22", "promoted_content": null }, 
						{ "name": "\u0635\u0648\u062a \u0627\u0644\u0645\u0637\u0631", "query": "%22%D8%B5%D9%88%D8%AA+%D8%A7%D9%84%D9%85%D8%B7%D8%B1%22", "url": "http:\/\/twitter.com\/search?q=%22%D8%B5%D9%88%D8%AA+%D8%A7%D9%84%D9%85%D8%B7%D8%B1%22", "promoted_content": null } 
					], 
					"as_of": "2015-03-19T11:19:49Z", 
					"created_at": "2015-03-19T11:15:05Z", 
					"locations": 
								[ 
									{ 
										"name": "Worldwide", 
										"woeid": 1 
									} 
								] 
	} 
]


The first few characters are: "[ {", which indicates an array. However, you access it as though the first character is "{".

I imagine the solution is as simple as:

JavaScript
var rawData = response;
var jsonObj = JSON.parse(response);

var name = jsonObj[0].trends[0].name;


Not sure if you've turned the returned string back into an object or not. You need to do that as shown above. The snippet I've posted assumes that response contains the text of the JSON. It should then set name to hold the text "#MukhangPeraAngPilipinas", which is the name held by the first trend in the array of them.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900