Saravanan
Saravanan

Reputation: 11592

how to declare two arrays in json?

I want to create a json string for my web application. Actually i am new to this json format.In my json string i have to create two arrays in my json structure.But i have some syntax problem in creating two arrays. my json string is given below for your reference.

{
    MarkUpdate:[
        {
            'FinalMarks':[
            {
                'studentId':'S1',
                'Ques_Mark':[
                {
                    'qId' :'Q1',
                    'mark':'14',                
                },
                {
                    'qId':'Q2',
                    'mark':'10',                
                }
                ]
            },          
            {
                'studentId':'S2',
                'Ques_Mark':[
                {
                    'qId' :'Q1',
                    'mark':'12',                
                },
                {
                    'qId':'Q2',
                    'mark':'13',                
                }               
                ]
            }
            ]
        }           
        ]
    }

In my above json string format,my MarkUpdate contains one array object named as FinalMarks.So,here i have to create one more array object named as EvalMarks under MarkUpdate.

Acually my EvalMarks contains following elements...

'EvalMarks':[
            {
                'EvalId':'E1',
                'Ques_Mark':[
                {
                    'qId'   :'Q1',
                    'studId':'S1',  
                    'mark':'13',                
                },
                {
                    'qId':'Q2',
                    'studId':'S1',  
                    'mark':'13',                                    
                }                   
                ]
            },

            {
                'EvalId':'E2',
                'Ques_Mark':[
                {
                    'qId'   :'Q1',
                    'studId':'S2',  
                    'mark':'10',                
                },
                {
                    'qId':'Q2',
                    'studId':'S2',  
                    'mark':'10',                                    
                }
                ]
            }
            ]

So, i have declare this EvalMarks under the MarkUpdate.I missed the syntax...

Could you plz tell me how to add this array object under the MarkUpdate.

guide me to get out of this issue...

Upvotes: 0

Views: 9706

Answers (2)

turiyag
turiyag

Reputation: 2887

To declare two arrays in one JSON object, remember that the JSON object can only be a single object, therefore the array must be inside the enclosing curly braces. For example:

{
  "array1":[1,2,3],
  "array2":["jim","louise","mark"]
}

For your case, it's important to remember that you should properly indent your braces, square and curly, so that you can visually identify mistakes before they become problems. I stringly recommend http://jslint.com/ to validate your JSON before using it. It's also great for Javascript:

{
    "MarkUpdate":[
        {
            "FinalMarks":[
                {
                    "studentId":"S1",
                    "Ques_Mark":[
                        {
                            "qId" :"Q1",
                            "mark":"14"
                        },
                        {
                            "qId":"Q2",
                            "mark":"10"
                        }
                    ]
                },
                {
                    "studentId":"S2",
                    "Ques_Mark":[
                        {
                            "qId" :"Q1",
                            "mark":"12"
                        },
                        {
                            "qId":"Q2",
                            "mark":"13"
                        }
                    ]
                }
            ]
        }
    ],
    "EvalMarks":[
        {
            "EvalId":"E1",
            "Ques_Mark":[
                {
                    "qId"   :"Q1",
                    "studId":"S1",
                    "mark":"13"
                },
                {
                    "qId":"Q2",
                    "studId":"S1",
                    "mark":"13"
                }
            ]
        },

        {
            "EvalId":"E2",
            "Ques_Mark":[
                {
                    "qId"   :"Q1",
                    "studId":"S2",
                    "mark":"10"
                },
                {
                    "qId":"Q2",
                    "studId":"S2",
                    "mark":"10"
                }
            ]
        }
    ]
}

Upvotes: 4

Ashwin Krishnamurthy
Ashwin Krishnamurthy

Reputation: 3758

Its not a valid JSON if you have commas after the last key-value pair in an object. I would start by knocking off all those unnecessary commas after the last key-value pairs inside most of your objects and validating the JSON in www.jslint.com

To be more clear ,

{
    "qId":"Q2",
    "studId":"S2",  
    "mark":"10",                                    
}

is Not Valid. On the other hand,

{
    "qId":"Q2",
    "studId":"S2",  
    "mark":"10"                                   
}

Is valid.

Upvotes: 0

Related Questions