简体   繁体   中英

How Do I See the Output of Changes to JSON

Updated to try to be more clear given the comments (Thank you for comments)

I apologize in advance for this question. I may just not have the vocabulary to properly research it. If I have an array of objects called restaurants stored in my project, for example: restaurants: [ {
"name": "joes's pizza", "url": "joespizza.com", "social properties": { "Facebook":"fb.com/pizza", "Instagram":"instagram.com/pizza", "googlePlus":"pizza+", "Twitter":"twitter.com/pizza" } }, {
"name": "tony's subs", "url": "tonys.com", "social properties": { "Facebook":"fb.com/subs", "Instagram":"instagram.com/subs", "googlePlus":"subs+", "Twitter":"twitter.com/subs" } }, {....} ]

I then run a function to add a unique idea to all the objects in the array. The result of console.log(restaurants) is this:

     {  
    "id": 3472, 
    "name": "joes's pizza",
    "url": "joespizza.com",
    "social properties": {
            "Facebook":"fb.com/pizza",
            "Instagram":"instagram.com/pizza",
            "googlePlus":"pizza+",
            "Twitter":"twitter.com/pizza"
        }
    },
     {   
    "id": 9987, 
    "name": "tony's subs",
    "url": "tonys.com",
    "social properties": {
            "Facebook":"fb.com/subs",
            "Instagram":"instagram.com/subs",
            "googlePlus":"subs+",
            "Twitter":"twitter.com/subs"
        }
    },
    {....}
]

I would now like to have this updated array of objects available to look at in my project, via the text editor, as a variable or restaurants.json file. How do I actually see the new modified json array and how do i save it so that i can work with it the same way i did this one above? I am currently doing this in the browser. I can see the results if i log it to the console but I need to be able to work with the new output. Thanks for taking the time to answer.

You can encode/decode JSON with JSON.stringify() and JSON.parse() .

Aside from converting to/from JSON, you work with standard JS objects and arrays:

var array = JSON.parse(json_str);
array[0].address = "5th Avenue";
console.log(JSON.stringify(array));

Well, there's really not enough information in your question but I assume a few things:

  1. You've loaded the json data from somewhere and it has been turned into a javascript object.
  2. You've edited the object somehow and wish to convert it back to json and save the changes.

Assuming the above to be true, you just need to serialize the object back to json and submit it back to your server where you can save it in any manner you deem appropriate.

  1. You can serialize the javascript object with JSON.stringify() (see https://stackoverflow.com/a/912247/4424504 )
  2. Add the serialized json to a hidden field on the form and submit it.
  3. On the server when processing the form submission, grab the data from the hidden field and do with it what you wish.
  4. Or get it back to the server any way you wish (ajax call, whatever) the key point is to serialize the object to a json string and save it

Hope that helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM