0

I am getting response in below format for every product and in a single call there can be many products. I am trying to access this data via jQuery but I'm not able to access it.

Productdata['someid'] =  { "Product Json data"}

I am using below syntax in jQuery but not getting the data. Please suggest.

alert(Productdata['someid']);
2
  • It's not Json formatted Commented Aug 1, 2014 at 9:28
  • thats not in the JSON format Commented Aug 1, 2014 at 9:31

3 Answers 3

1

Its not going as JSON format .

JSON is a key : value pair format ;

so your Productdata should be in below format:

Productdata =  { 'someid' : "Product Json data"}
Sign up to request clarification or add additional context in comments.

Comments

0

A Json like this

var data={"name":"somebody"};

To call

return data.name

Or

return data["name"]

Comments

0

The problem here is that JavaScript does not support associative arrays (scroll down to "Associative arrays, no way!"). It has some internal workarounds which make it appear as if it does, but really all it does is just adding the keys as properties.

So you would most likely be able to access it using Productdata.someid = ....

EDIT: So assuming you have the following JSON string: {"id":"123"} (which is valid JSON), you can use it like this:

var jsonString = '{"id":"123"}';
var parsedJSON = $.parseJSON(jsonString);
var productID = "product_" + parsedJSON.id;

Does this help?

Some useful links: JSON format checker to make sure the JSON is valid.

Unfortunately I wasn't allowed to add more than 2 links, so the jQuery parseJSON function link is still in the comment below.

4 Comments

thanks for your reverts , if I render json like Product_1 = {Json data} Product_2 so how I will access it in jquery method because I will be having id = 1 or 2 etc Now I will make a variable var id = "value from a hidden field" (assume 1) var variabletofetch = "Product_"+id Now variable to fetch will be Product_1 now how I will access this value as a variable ?
@user3592182 Are you getting valid JSON? Are you using the proper stringify/parse functions within jQuery? Try and use a JSON format checker to make sure the JSON is valid.
@user3592182 Has your problem been solved? If so it would be nice if you marked one of the responses as an answer (if it was, of course). Otherwise, let us know what the continued problem seems to be.
Problem was resolved by adding var productJson = new Array() in my js file . Thanks for your support :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.