1

i want to create J SON object in angular2 like this:

{ "classifications": [ 
                      { "type1" : [{"name":"text"},{"name":"text2"},{"name":"text2"}]} , 
                      { "type2" : [{"name":"image"},{"name":"img2"},{"name":"img3"},{"name":"img4"}]} ,
                      { "type3" : [{"name":"mp3"}]}
                      ] 
        }

is this the right way or not?? if not then how to make it and how to get data from this J SON object??

4
  • What is not working? Do you get an error? Commented Oct 20, 2016 at 9:45
  • No, just i don't sure about this way and i don't know how to get data from it to test Commented Oct 20, 2016 at 9:59
  • You don't need the " for the keys but besides that it should be fine. Commented Oct 20, 2016 at 10:02
  • For example i want to get values of type1 or just the first value of type1 Commented Oct 20, 2016 at 10:03

1 Answer 1

2
let x = { "classifications": [ 
                      { "type1" : [{"name":"text"},{"name":"text2"},{"name":"text2"}]} , 
                      { "type2" : [{"name":"image"},{"name":"img2"},{"name":"img3"},{"name":"img4"}]} ,
                      { "type3" : [{"name":"mp3"}]}
                      ] 
        }

console.log(x.classifications[0].type1[0].name);
Sign up to request clarification or add additional context in comments.

4 Comments

this make an error : Property 'type1' does not exist on type '{ "type1": { "name": string; }[]; } | { "type2": { "name": string; }[]; } | { "type3": { "name": ...'.
thanks really it works , but i don't know why eclipse marks it as an syntax error
You can try console.log(x['classifications'][0]['type1'][0]['name']);
If this answers your question, you can accept it using the white checkmark below the up-down-vote buttons to indicate that your problem is solved. Thanks.

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.