Appending a key value pair to a json object

后端 未结 4 1980
挽巷
挽巷 2020-12-16 00:48

This is the json object I am working with

{
    \"name\": \"John Smith\",
    \"age\": 32,
    \"employed\": true,
    \"address\": {
              


        
4条回答
  •  不知归路
    2020-12-16 01:33

    You need to make an object at reference "collegeId", and then for that object, make two more key value pairs there like this:

    var concattedjson = JSON.parse(json1);
    concattedjson["collegeId"] = {};
    concattedjson["collegeId"]["eventno"] = "6062";
    concattedjson["collegeId"]["eventdesc"] = "abc";
    

    Assuming that concattedjson is your json object. If you only have a string representation you will need to parse it first before you extend it.

    Edit

    demo for those who think this will not work.

提交回复
热议问题