简体   繁体   中英

Saving JSON data to database

Picked up knockout.js and I'm trying to save data to a mysql database using PHP(Laravel framework to be more specific). Here is the ajax:

$.ajax("save", {
  data: ko.toJSON(this), // turns the input into JSON I assume?
  type: "post",
  success: function() {
    alert("Success!");  // right now I'm alerting the data to troubleshoot
  }
});

And my method to save the data into the DB:

// I expect my ajax to send me data that is in json format
// I then decode the data to get an array which I can use to run my query
$data = json_decode(Input::json(), true);
return DB::table('content')->insert($data);

However, the problem is that I seem to be receiving data of the object type(ran gettype() on $data and json_decode() returned an error as well), stdClass Object to be precise. Upon troubleshooting what was happening within my javascript I alerted the data and it was in JSON so that must be working.

I did get it working however like this:

$data = json_encode(Input::json(), true);
return DB::table('content')->insert(json_decode($data), true);

This worked successfully, save to the database etc. however I am confused. Pardon my inexperience with JSON, but shouldn't the process be:

  • Encode data into JSON on the front-end
  • Send data to server
  • Decode data on back-end, turning it into a format that can be handled by the server(an array in this case)
  • Insert data

So, in my first attempt that didn't work $data = Input::json() is of type object. Json_decode throws an error because it expects a string and now I'm kinda lost because I expect JSON.

When you use Input::json() Laravel automatically decodes the JSON into an object for you to work with. In most cases you would want to do some work with the JSON submitted, and would not usually store it as JSON but rather in separate columns of your database row.

If you're using Laravel 3 you can instead use the following line to get the raw (undecoded) JSON:

$raw = Request::foundation()->getContent();

In Laravel 4 I believe that it is:

$raw = Input::getContent();

Alternatively, if you want the JSON decoded as an array you can use the following in Laravel 3...

$array = Input::json(true);

There's no as-array equivalent in Laravel 4.

So, I've found A solution to this. Apparently Input::json() returns data of the type of object, PHP's generic object, empty normally. All the data is stored inside that object and the only thing left to do was turn that object into an array. get_object_vars() does exactly that the result will be a nicely formatted array, this is what worked:

$data = get_object_vars(Input::json()); // will return an array, exactly what I need
return DB::table('tableName')->insert($data); // Data can be inserted now properly

If you're trying to transmit your knockout view model as json, you won't be able to directly convert your view model to json. The reason is that all of the fields in your view model aren't actually fields like this:

var viewmodel = {
    name: 'Bob',
    age: 23
};

Instead, the fields are knockout observables:

var viewmodel = {
    name: ko.observable('Bob'),
    age: ko.observable(23)
};

You actually need to call each observable to get it's current value. Obviously, this could be tedious, but there's a mapping plugin that can do this for you -- the Knockout mapping plugin .

With the mapping plugin, you can simply do:

ko.mapping.toJSON(viewmodel)

And it will return a json string that represents the viewmodel as a regular js object.

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