php - What is the simplest method of programmatically adding data to JSON? -


for php/html page simplest method of adding data json? should using php, js, or jquery?

i've tried different lots of different methods found online, can't work. i've tried of these can't quite right.

 var myobject = new object();  json.stringify()  json.parse()  $.extend();  .push()  .concat() 

i have json file loaded

    {"commentobjects":          [                 {"thecomment": "abc"},                 {"thecomment": "def"},                 {"thecomment": "ghi"}             ]     } 

i want programmatically add

    var thisnewcomment = 'jkl;     {"thecomment": thisnewcomment} 

so json variable be

    {"commentobjects":          [                 {"thecomment": "abc"},                 {"thecomment": "def"},                 {"thecomment": "ghi"},             {"thecomment": "jkl"}         ]     } 

/////////////////// edit after answer //////////////////

this ajax in index.php file used call php function (in separate file):

    function commentsaveaction ()     {         var mytext = $(".mycommentinput").val();         mytext = mytext.replace("\"","'");              $.ajax({                 url: 'php/commentwrite.php',                 data: { thephpdata: mytext },                 success: function (response) {                }             });     } 

and finished php function used of deceze:

    <?php      function writefunction ()      {         $filename = '../database/comments.txt';          $arr = json_decode(file_get_contents($filename),true);         $mydata = $_get['thephpdata'];            $arr['commentobjects'][] = array('thecomment' => $mydata);         $json = json_encode($arr);          $filewrite=fopen($filename,"w+");         fwrite($filewrite,$json);         fclose($filewrite);     }     writefunction ();         ?> 

////////////////////// js , not php /////////////////////

        var myjsondata;          function getcommentdata ()         {             $.getjson('database/comments.txt', function(data) {                  myjsondata = data;                  var count = data.commentobjects.length;                 (i=0;i<count;i++) {                     $(".commentbox ul").append("<li>"+data.commentobjects[i].thecomment+"</li>");                 }             });          }          function commentsaveaction ()         {             var mytext = $(".mycommentinput").val();             mytext = mytext.replace("\"","'");              myjsondata.commentobjects.push({"thecomment": mytext});              var count = myjsondata.commentobjects.length;             $(".commentbox ul").append("<li>"+myjsondata.commentobjects[count-1].thecomment+"</li>");         } 

whichever language in, have parse json string object/array, modify it, encode json string. don't attempt direct string manipulation of json string. php example:

$arr = json_decode($json, true); $arr['commentobjects'][] = array('thecomment' => 'jkl'); $json = json_encode($arr); 

whether in javascript or php or elsewhere depends on when/why/where need this; that's impossible without knowing more use case.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -