简体   繁体   English

将JSON数据保存到数据库

[英]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). 拿起knockout.js,我正在尝试使用PHP将数据保存到mysql数据库(Laravel框架更具体)。 Here is the ajax: 这是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: 我的方法是将数据保存到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. 但是,问题是我似乎正在接收对象类型的数据gettype()$data上运行gettype()并且json_decode()返回错误), stdClass Object是精确的。 Upon troubleshooting what was happening within my javascript I alerted the data and it was in JSON so that must be working. 在对我的javascript中发生的事情进行故障排除时,我提醒了数据并且它是在JSON中,因此必须正常工作。

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: 请原谅我对JSON的经验不足,但不应该是这个过程:

  • Encode data into JSON on the front-end 将数据编码到前端的JSON中
  • 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. 所以,在我第一次尝试不起作用时, $data = Input::json()属于object类型。 Json_decode throws an error because it expects a string and now I'm kinda lost because I expect JSON. Json_decode抛出一个错误,因为它需要一个字符串,现在我有点丢失,因为我期待JSON。

When you use Input::json() Laravel automatically decodes the JSON into an object for you to work with. 当您使用Input::json() Laravel会自动将JSON解码为一个对象供您使用。 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. 在大多数情况下,您可能希望使用提交的JSON进行一些工作,并且通常不会将其存储为JSON,而是存储在数据库行的单独列中。

If you're using Laravel 3 you can instead use the following line to get the raw (undecoded) JSON: 如果您使用的是Laravel 3,则可以使用以下行来获取原始(未解码)JSON:

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

In Laravel 4 I believe that it is: 在Laravel 4中我相信它是:

$raw = Input::getContent();

Alternatively, if you want the JSON decoded as an array you can use the following in Laravel 3... 或者,如果您希望将JSON解码为数组,则可以在Laravel 3中使用以下内容...

$array = Input::json(true);

There's no as-array equivalent in Laravel 4. Laravel 4中没有as-array等价物。

So, I've found A solution to this. 所以,我找到了解决方案。 Apparently Input::json() returns data of the type of object, PHP's generic object, empty normally. 显然, Input::json()返回对象类型的数据,PHP的通用对象,通常为空。 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: get_object_vars()完全相同,结果将是一个格式良好的数组,这是有效的:

$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. 如果您尝试将淘汰视图模型作为json传输,则无法将视图模型直接转换为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. 实际上,您需要调用每个observable来获取它的当前值。 Obviously, this could be tedious, but there's a mapping plugin that can do this for you -- the Knockout mapping plugin . 显然,这可能很乏味,但是有一个映射插件可以为你做这个 - Knockout映射插件

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. 它将返回一个json字符串,表示viewmodel为常规js对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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