JSON在线解析

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
**全国地表水水质监测系统 断面点位数据集** 发布于:2024-03-02 18:33 AliRedPacket首页红包 发布于:2023-12-23 15:41 main主程序 发布于:2023-12-23 15:18 扬秋OL(autojs) 发布于:2023-12-23 15:43 json教师各项分数 发布于:2023-12-09 16:39 员工工作量 发布于:2023-11-07 15:17 浪潮POST 发布于:2023-10-30 11:22 报账单信息 发布于:2023-10-20 14:32 我带会员单位岁啊哈幅度缺乏撒网缺乏v 发布于:2022-09-27 08:28 环保token 发布于:2022-05-12 21:12 流水线的保存的json数据,把里面所有的job的几个信息摘出来,取出每个job的jobName、jobType、categoryId、realJob 内容。 发布于:2022-04-22 19:17 爱上CAD 发布于:2022-02-17 23:03 中国省市区 json 发布于:2021-10-15 13:39 供应商带出币种 发布于:2021-07-23 10:19 拉取采购收货 发布于:2021-07-13 16:14 解析IP地址 发布于:2021-04-12 20:45 OA --> CMDB_DCOS的北向数据 发布于:2021-02-02 15:00 测试json 是否正确 发布于:2021-01-13 11:06 python题库 发布于:2021-01-06 10:31 json 解析 发布于:2020-10-18 15:05 电池图所需数据 发布于:2020-10-08 18:32 电池图所需数据 发布于:2020-09-20 13:57 电池图所需数据 发布于:2020-09-20 13:57 JSON在线解析,JSON格式化 发布于:2020-09-16 12:10 电池图所需数据 发布于:2020-09-02 11:35 电池图所需数据 发布于:2020-08-25 17:50 建立一个mock测试 json 数据。 发布于:2020-08-22 10:32 地理缩写对照 发布于:2020-07-08 13:28 v2ray-config 发布于:2021-05-11 13:03 JSON在线解析,JSON格式化 发布于:2020-06-20 15:04 [更多]
显示目录

使用JS构建JSON格式字符串



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

使用JavaScript构建JSON格式字符串实现步骤

本文将帮你使用javascript来创建json格式字符串。

通过jQuery插件$.toJSON把数据对象转换为json格式。

使用JavaScript构建JSON格式字符串

JavaScript代码

在这里包含了javascript代码。$(“#form”).submit(function(){}- delete_button是form标签的ID,通过element.val()调用表单输入框的值。

代码如下:

<script src="jquery.min.js"></script> 
<script src="jquery.json-2.2.js"></script> 
<script src="GetPostAjax.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() 
{ 
$("#form").submit(function(e){ 
e.preventDefault(); 
var username,email,password,gender; 
username=$("#username").val(); 
email=$("#email").val(); 
password=$("#username").val(); 
gender=$("#gender").val(); 
if(username.length>0 && email.length>0 && password.length>0 &&gender.length>0) 
{ 
//Creating Objects 
var request = new Object(); 
var userDetails = new Object(); 
var user = new Object(); 
var websites=new Array(); 
user.name=username; 
user.email=email; 
user.password=password; 
user.gender=gender; 
//Array Push 
if(website1.length>0) 
websites.push(website1); 
if(website2.length>0) 
websites.push(website2); 
if(website3.length>0) 
websites.push(website3); 
user.websites=websites; 
userDetails.user = user; 
request.userDetails = userDetails; 
var jsonfy = $.toJSON(request); 
// Encodes special characters 
var encodedata = 'jsondata='+encodeURIComponent(jsonfy); 
//Ajax Call 
var url='website API URL'; 
post_data(url,encodedata, function(data) { 
alert("Success"); 
}); 
} 
}); 
}); 
</script"> 

HTML代码

代码如下:

<form method='post' action='' id='form'> 
Name 
<input type='text' name='username' id='username' /> 
Email 
<input type='text' name='email' id='email' /> 
Password 
<input type='text' name='password' id='password' /> 
Gender 
<select name='gender' id='gender'><option value='male'>Male</option><option value='female'>Female</option></select> 
Websites 
<input type='text' id='website1' /> 
<input type='text' id='website2' /> 
<input type='text' id='website3' /> 
<input type='submit' id='submit'/> 
</form> 

JSON输出

代码如下:

{ 
    "userDetails":{ 
    "user":{ 
    "name":"Srinivas Tamada", 
    "email":"srinivas@9lessons.info", 
    "password":"Srinivas Tamada", 
    "gender":"male", 
    "websites":["www.software8.co","www.heatpress123.net","www.0769zzw.com"] 
    } 
  } 
}

JSON Encoded

对特殊字符进行编码,会把以下字符进行编码

, / ? : @ & = + $ # 
jsondata=%7B%22userDetails%22%3A%7B%22user%22%3A%7B%22name%22%3A%22Srinivas%20Tamada%22%2C%22email%22%3A%22srinivas%409lessons.info%22%2C%22password%22%3A%22Srinivas%20Tamada%22%2C%22gender%22%3A%22male%22%2C%22websites%22%3A%5B%22www.9lessons.info%22%2C%22www.egglabs.in%22%2C%22www.fglogin.com%22%5D%7D%7D%7D

GetPostAjax.js

定义jquery的ajax请求方法

function post_data(url,encodedata, success){ 
$.ajax({ 
type:"POST", 
url:url, 
data :encodedata, 
dataType:"json", 
restful:true, 
contentType: 'application/json', 
cache:false, 
timeout:20000, 
async:true, 
beforeSend :function(data) { }, 
success:function(data){ 
success.call(this, data); 
}, 
error:function(data){ 
alert("Error In Connecting"); 
} 
}); 
}

java创建json对象

// 声明两个json数组  
[java] view plain copy print?
      JSONArray gResTable = new JSONArray();   
      JSONArray gCmtTable = new JSONArray();   
// 声明json对象       
ONObject outData = new JSONObject();  
//把json数组加到json对象中  
[java] view plain copy print?
outData.put("ResTable", gResTable);  
outData.put("CmtTable", gCmtTable);  
  //此时创建出来的如下:jsonData={"ResTable":[],"CmtTable":[]};  
  把json数据加到json数组中  
for (int i = 0; i < 2; i ++)  
{  
    JSONObject node = new JSONObject();  
    node.put("thumb_path", "./Image/" + i +".gif");  
    node.put("flash_path", "./Image/" + i +".gif");  
    node.put("desc1", "可疑车辆" + i);  
    node.put("desc2", "");  
    node.put("desc3", "");  
    node.put("desc4", "");  
    node.put("title", "hello");  
    node.put("upload_time", (new java.util.Date()).toString());  
    node.put("uploader", "王二");  
    gResTable.put(node);  
}  

for (int i = 0; i < 2; i ++)  
{  
    JSONObject node = new JSONObject();  
    node.put("logo_path", "./Image/" + i +".gif");  
    node.put("comment", "hello");  
    node.put("comment_time", (new java.util.Date()).toString());  
    node.put("author", "王二");  
    gCmtTable.put(node);  
}

json格式如下:

[java] view plain copy print?
/* 
    jsonData={"ResTable":[ 
    {"upload_time":"Sat May 26 20:16:37 CST 2012","title":"hello","flash_path":"./Image/0.gif","uploader":"王二","thumb_path":"./Image/0.gif","desc3":"","desc4":"","desc1":"可疑车辆0","desc2":""}, 
    {"upload_time":"Sat May 26 20:16:37 CST 2012","title":"hello","flash_path":"./Image/1.gif","uploader":"王二","thumb_path":"./Image/1.gif","desc3":"","desc4":"","desc1":"可疑车辆1","desc2":""}], 
    "CmtTable": 
    [{"author":"王二","logo_path":"./Image/0.gif","comment":"hello","comment_time":"Sat May 26 20:16:37 CST 2012"}, 
    {"author":"王二","logo_path":"./Image/1.gif","comment":"hello","comment_time":"Sat May 26 20:16:37 CST 2012"} 
 ]}; 
*/
由JSRUN为你提供的JSON在线运行、在线编译工具
        JSRUN提供的JSON 在线运行,JSON 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout