如何将json模型对象保存到php mysql数据库 [英] how to save json model object to php mysql database

查看:141
本文介绍了如何将json模型对象保存到php mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,因此,如果我错过了任何内容,请耐心等待:)

this is my first post so if im missing anything please be patient :).

使用来自 GoJS 的go-debug.js库在浏览器上绘制一些图表但是我在保存选项上遇到了问题.此图产生一个JavaScript对象,您可以通过此命令 myDiagram.model.toJson()将其转换为Json并将其传递给数据库.我想按保存按钮使用 Ajax 方法将图保存到 PhP ,然后保存到 mySQL 数据库中.

im using the go-debug.js library from GoJS to make some diagrams on the browser but im running an issue with the saving option. This diagram produce a JavaScript object which you can converted to Json and pass it to the Database by this command myDiagram.model.toJson() . I want when i press the save button to save the diagram using the Ajax method to PhP and then into mySQL database.

谢谢您的帮助!

这是我的保存脚本,但是我不知道为什么它不起作用.

here is my save script but i can't figure out why it doesnt work.

  window.onload = function(){


jQuery(document).ready(function(){
    $.ajax({
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    url: 'savemodel.php',
    data: {json: JSON.stringify(modelJson)},
    dataType: 'json'
})

.done( function( data ) {
    console.log('done');
    console.log(data);
})
.fail( function( data ) {
    console.log('fail');
    console.log(data);
})
});

    //return modelJson;



  }
  function load() {
    myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);
    // loadDiagramProperties gets called later, upon the "InitialLayoutCompleted" DiagramEvent
  }


  function loadDiagramProperties(e) {
    var pos = myDiagram.model.modelData.position;
    if (pos) myDiagram.position = go.Point.parse(pos);
  }



  }

我的php代码是:

      <?php 

header('Content-Type: application/json');
include 'config.php';
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

$session_id=$_SESSION['sess_user_id']; // User session id

$modelJson = $_POST['json'];




$sql = "INSERT INTO c_map 
       (ref_num, members_id, title, description, ingredients) 
        VALUES (NULL, '$session_id', 'title', 'description',
        '".mysql_real_escape_string($modelJson)."');";


mysqli_query($connection, $sql);





?>

例如命令myDiagram.model.toJson();的产生;这是一个有两个孩子的树形图,如下所示:

eg. The produce of the command myDiagram.model.toJson(); which is a tree diagram with 2 children will look like this:

{ "class": "go.GraphLinksModel",
  "modelData": {"position":"-545.114064532096 -44.69966023522102"},
  "nodeDataArray": [ 
{"key":"Alpha"},
{"key":"N"},
{"key":"N2"}
 ],
  "linkDataArray": [ 
{"from":"Alpha", "to":"N"},
{"from":"Alpha", "to":"N2"}
 ]}

我引用了GoJs网站上的模型保存文档:

I quote the model save documentation from the GoJs site:

GoJS不需要您将模型保存在任何特定的介质或 格式.但是因为这是JavaScript,而JSON是最受欢迎的 数据交换格式,我们可以轻松编写和读取模型 作为JSON格式的文本.

GoJS does not require you to save models in any particular medium or format. But because this is JavaScript and JSON is the most popular data-interchange format, we do make it easy to write and read models as text in JSON format.

只需调用Model.toJson即可生成表示您的模型的字符串. 调用静态方法Model.fromJson构造并初始化一个 给定模型的字符串,该字符串由Model.toJson生成.许多样本 演示这一点-搜索名为"save"的JavaScript函数,然后 加载".这些功能大多数都在页面上写入和读取TextArea 本身,以便您可以查看和修改JSON文本,然后加载它 得到一个新的图.但是在编辑时请谨慎,因为JSON 语法非常严格,任何语法错误都会导致这些负载" 功能失败.

Just call Model.toJson to generate a string representing your model. Call the static method Model.fromJson to construct and initialize a model given a string produced by Model.toJson. Many of the samples demonstrate this -- search for JavaScript functions named "save" and "load". Most of those functions write and read a TextArea on the page itself, so that you can see and modify the JSON text and then load it to get a new diagram. But please be cautious when editing because JSON syntax is very strict, and any syntax errors will cause those "load" functions to fail.

JSON格式的文本对您所用的数据类型有严格的限制 无需额外假设即可表示.保存和加载任何 您在节点数据(或链接数据)上设置的数据属性,它们 需要满足以下要求:

JSON formatted text has strict limits on the kinds of data that you can represent without additional assumptions. To save and load any data properties that you set on your node data (or link data), they need to meet the following requirements:

该属性是可枚举的,并且其名称不能以 下划线(您可以使用以 下划线,但不会保存)属性值不是 未定义且不是函数(JSON无法如实保存 函数),模型知道如何将属性值转换为JSON 格式(数字,字符串,JavaScript数组或纯JavaScript 对象或对象的属性值形成一棵树 结构-没有共享或循环引用Model.toJson和 Model.fromJson还将处理Point,Size,Rect,Spot, 边距,几何和非图案画笔.但是我们建议 您将那些对象存储在它们的字符串表示形式中, 类的解析和字符串化静态函数.

the property is enumerable and its name does not start with an underscore (you can use property names that do start with an underscore, but they won't be saved) the property value is not undefined and is not a function (JSON cannot faithfully hold functions) the model knows how to convert the property value to JSON format (numbers, strings, JavaScript Arrays, or plain JavaScript Objects) property values that are Objects or Arrays form a tree structure -- no shared or cyclical references Model.toJson and Model.fromJson will also handle instances of Point, Size, Rect, Spot, Margin, Geometry, and non-pattern Brushes. However we recommend that you store those objects in their string representations, using those classes' parse and stringify static functions.

由于您使用的是JavaScript,因此添加数据非常简单 节点数据的属性.这使您可以关联任何内容 每个节点所需的信息.但是如果您需要关联一些 与模型相关的信息,即使没有 根本没有节点数据,您可以向Model.modelData添加属性 目的.该对象的属性将由Model.toJson和 由Model.fromJson读取,就像写入节点数据对象并 阅读.

Because you are using JavaScript, it is trivial for you to add data properties to your node data. This allows you to associate whatever information you need with each node. But if you need to associate some information with the model, which will be present even if there is no node data at all, you can add properties to the Model.modelData object. This object's properties will be written by Model.toJson and read by Model.fromJson, just as node data objects are written and read.

推荐答案

您不需要json_decode此JSON,因为它已成为对象.
删除此部分:

You don't need to json_decode this JSON, because it's becoming an Object.
Remove this part:

$directions = json_decode($_POST['json']);
var_dump(directions); // here's an error btw

并将其更改为此:

$directions = $_POST['json'];

然后将其保存为文本模式.
别忘了逃脱它:

Then just save it in text mode.
Don't forget to escape it:

$sql = "INSERT INTO c_map 
       (ref_num, members_id, title, description, ingredients) 
        VALUES (NULL, '$session_id', 'title', 'description',
        '".mysql_real_escape_string($directions)."');";

这篇关于如何将json模型对象保存到php mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆