本帖最后由 vitalgg 于 2025-5-20 15:30 编辑
#+title:混合编程操作json数据
* 背景
JSON数据格式已成为大多数编程语言的内置的数据格式。它可以方便地在所使用的语言中的对象及数据和字符串之间进行转化。
autolisp本身没有对象。但是有点对表,列表。这种格式与json非常相似。但是Autolisp中的整数和浮点数精度及范围有限,在相互转化时数值类型的数据会失真。
为了保持数据的一致性。我们可以调用外部程序进行处理,如python。
windows下的大多数CAD支持visuallisp,现在就用 winhttp.httprequest 做为中介让autolisp与python进行交互编程。
* 服务端
** python 版本
jsonserver.py 内容:
#+begin_src python
 - from flask import Flask,jsonify,request
- import json
- jsonobj = {};
- app = Flask(__name__)
- @app.route('/api/json',methods=['GET'])
- def get_json():
- global jsonobj
- return json.dumps(jsonobj)
- @app.route('/api/json',methods=['POST'])
- def init_json():
- global jsonobj
- jsonobj = request.json;
-
- return json.dumps(jsonobj)
- @app.route('/api/json_set',methods=['POST'])
- def set_json():
- global jsonobj
- data = request.json;
- for key in data:
- jsonobj[key] = data[key]
- return json.dumps(jsonobj)
- if __name__ == '__main__':
- app.run(debug=True)
#+end_src
安装python及需要的包
#+begin_src shell
winget install python.python.3.13
pip install flask
#+end_src
运行服务端
#+begin_src shell
python jsonserver.py
#+end_src
** nodejs 版本
- const http = require('http');
- // const url = require("url");
- const port = 5000;
- let jsonobj = {};
- http.createServer((req, res) => {
- const {url ,method} = req;
-
- res.statusCode = 200,
- res.setHeader('Content-Type', 'text/plain;charset=utf-8');
- if( req.url === '/api/json' && req.method === 'GET') {
- toGet(req, res);
- }else if( req.url === '/api/json' && req.method === 'POST') {
- toPost(req, res);
- }else if( req.url === '/api/json_set' && req.method === 'POST') {
- toSetPost(req, res);
- }
-
- }).listen(port, () => {
- console.log(`Server listening on: http://localhost:${port}`);
- });
- //获取GET请求内容
- function toGet(req, res){
- res.end(JSON.stringify(jsonobj));
- console.log(JSON.stringify(jsonobj));
- }
- //获取POST请求内容、cookie
- function toPost(req, res){
- let data = '';
- req.on('data', function(chunk){
- data += chunk;
- });
- req.on('end', function(){
- jsonobj = JSON.parse(data);
- res.end(JSON.stringify(jsonobj));
- console.log(jsonobj);
- });
- }
- function toSetPost (req, res){
- let data = '';
- req.on('data', function(chunk){
- data += chunk;
- });
- req.on('end', function(){
- reqdata = JSON.parse(data);
- for(var key in reqdata){
- jsonobj[key]=reqdata[key];
- };
- res.end(JSON.stringify(jsonobj));
- });
- }
复制代码
* 客户端
@lisp 内置了 http 交互函数,可以用 @::@get , @::postjson 与服务端进行交互。
没有安装 @lisp的可以执行以下代码,网络加载需要的函数。
 - (progn(vl-load-com)(setq s strcat h"http"o(vlax-create-object (s"win"h".win"h"request.5.1"))v vlax-invoke e eval r read)(v o'open "get" (s h"://""atlisp.""cn/cloud"):vlax-true)(v o'send)(v o'WaitforResponse 1000)(e(r(vlax-get o'ResponseText))))
默认端口是5000,你也可以修改,只要保证服务端与客户端一致就可以了。
** 初始化json对象
#+begin_src lisp
 - (@::postjson "http://localhost:5000/api/json" "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}")
设置键值
 - (@::postjson "http://localhost:5000/api/json_set" "{\"k1\":\"aaa\"}")
取 json,当然上面的设置后也会直接返回结果
 - (@::@get "http://localhost:5000/api/json")
原文:https://gitee.com/atlisp/atlisp- ... 8D%E4%BD%9Cjson.org
|