0

I need to replace a value in the response ,

My Json format :

{"Message":"","IsSuccessful":true,"sample2.0com":["data1","data2"],"sample1.0com":["data3","data4","data5","data6"]}}

When I am trying to read the above response , I am facing issue

let sample2 = response.data.Result.sample2.0com;

Please give me a solution to read sample2.0com components from the response

1
  • 1
    Did you try this response.data.Result['sample2.0com']? Commented May 21, 2018 at 6:13

3 Answers 3

2

Keys that are not valid JavaScript variable names (start with one of a-zA-Z_$ and contain only a-zA-Z0-9_$) must be accessed as strings inside square brackets ([]).

For example: obj.camelCase is OK, but obj.this key contains spaces is not OK - it would have to be obj['this key contains spaces'].

Sign up to request clarification or add additional context in comments.

Comments

2

JSON objects are surrounded by curly braces {}.

You can access the object values by using dot . notation:

myObj = { "name":"John", "age":30, "car":null };
x = myObj.name;

You can also access the object values by using bracket ([]) notation:

myObj = { "name":"John", "age":30, "car":null };
x = myObj["name"];

Similarly you can use as like above

responseData = {"Message":"","IsSuccessful":true,"sample2.0com":["data1","data2"],"sample1.0com":["data3","data4","data5","data6"]}}

let sample2 = responseData["sample2.0com"];

This will fix yours

Comments

1

Is because of your . in the sample2.0com.. you have to use the [] to call the property, so use response.data.Result['sample2.0com'], but I am not sure what is the "Result" object ... maybe you should use response.data['sample2.0com']

2 Comments

Thank u , it worked with response.data['sample2.0com']
Great, please mark the answer as a good one, so other peoples with the same problem will know it's a solution.

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.