如何将json字符串json转化为字符串变量

当 JSON 的键为变量而不是字符串时,怎么写? - 知乎15被浏览<strong class="NumberBoard-itemValue" title="1分享邀请回答1添加评论分享收藏感谢收起257 条评论分享收藏感谢收起写回答用JSON表示一个数组对象,使用“[ ]“包含所有元素,每个元素用逗号分隔,元素可以使任意的value,例如,一下数组包含了一个String、Number、Boolean、和一个
null:
[“abc”,123,false,null]
//*********************JSONObject转化字符串**************************
// 调用执行的方法
int flag = factoryService.updateValidFlag(fty);
String msg="['更新成功']";
if(flag&=0){
&&& msg="['更新失败']";
&&& JSONArray json = (JSONArray)JSONSerializer.toJSON(msg);
&&& ServletActionContext.getResponse().setCharacterEncoding("utf-8");
ServletActionContext.getResponse().getWriter().print(json.toString());
}catch (Exception e) {
&&& e.printStackTrace();
//************************JSONObject转化对象*********************************
int fid = Integer.parseInt(ServletActionContext.getRequest().getParameter("id"));
factory = factoryService.queryFactoryById(fid);
JSONArray json=JSONArray.fromObject(factory);
ServletActionContext.getResponse().setCharacterEncoding("utf8");
ServletActionContext.getResponse().getWriter().print(json.toString());
//************************JSONObject转化list*********************************
// 调用查询的方法并接收查询到的结果集
listFactory = .......;
JSONArray json = JSONArray.fromObject(listFactory);
// 设置编码
ServletActionContext.getResponse().setCharacterEncoding("utf-8");
// 写入到前台
ServletActionContext.getResponse().getWriter().print(json.toString());
前台中接收转化的值就可以了
function updateValidFlag(values){
//获得rid的值
var rid = $("input:radio[name='ra']:checked").val();
type:"post",
url:"/factory/factoryAction!updateValidFlag.action",
data:{"fid":fid,"rid":rid},
dataType:"json",
async:false,
success:function(json){
// 在action中返回的msg的信息& 更新成功或更新失败
alert(json);&
&&&&&&&&&&&&&&&&&&&&& // 更新刷新
self.location.reload(false);
浏览: 1427 次
来自: 武汉
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'博客分类:
List对象转成Gson字符串(两种方式)
(1)使用Gson gson=new Gson()类中的& gson.toJson(list);方法
案例 (ajax+json+jquery 省市县级联):
后台代码:
package com.zz.
import java.io.IOE
import java.io.PrintW
import java.util.ArrayL
import java.util.L
import javax.servlet.ServletE
import javax.servlet.http.HttpS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import org.omg.IOP.E
import com.google.gson.G
public class AjxServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码格式
response.setContentType("application/charset=utf-8");
int province = Integer.parseInt(request.getParameter("province"));
int city = Integer.parseInt(request.getParameter("city"));
System.out.println("province==&" + province);
System.out.println("city==&" + city);
//list对象
List list = new ArrayList();
if (province != 0) {
if (province == 1) {
TestJson tj = new TestJson();
//第一个对象
tj.setCityName("郴州市");
tj.setCityNo("10");
&&&&&&&&&&&&&&& //第二个对象
TestJson tj2 = new TestJson();
tj2.setCityName("长沙市");
tj2.setCityNo("11");
//分别将对象加入到list中
list.add(tj);
list.add(tj2);
if (province == 2){
TestJson tj = new TestJson();
tj.setCityName("武汉市");
tj.setCityNo("20");
TestJson tj2 = new TestJson();
tj2.setCityName("十堰市");
tj2.setCityNo("21");
list.add(tj);
list.add(tj2);
if(city!=0){
if(city==10){
TestJson tj = new TestJson();
tj.setCountyNo("101");
tj.setCountyName("苏仙区");
TestJson tj2 = new TestJson();
tj2.setCountyNo("102");
tj2.setCountyName("北湖区");
list.add(tj);
list.add(tj2);
PrintWriter out = response.getWriter();
Gson gson = new Gson();
String reslut = gson.toJson(list);
& System.out.println("reslut:" + reslut);
&&&&& response.setHeader("pargma", "no-cache");
&&&&&&&&& response.setHeader("cache-control", "no-cache");
& out.println(reslut);
& out.flush();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
&&&&&& doGet(request, response);
前台jsp界面:
&%@ page language="java" import="java.util.*" pageEncoding="utf-8"%&
&%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%&
&html&
&head&
&title&无标题文档&/title&
& &script type="text/javascript" src="js/jquery-1.4.2.js"&&/script&
&script type="text/javascript"&
function sub(){
&&&&&
&&&&& $.ajax({
type: "post",
url : 'AjxServlet',
data: { province:$('#province').val(), city:$('#city').val()},
success : function(result){
alert(result);
if(eval(result)) {
var html="&option value='11111' &--请选择--&/option&";
for(var i=0;i&result.i++){
var TestJson =result[i];
&&&&&&&&&&& var id=TestJson.cityNo;
&&&&&&&&&&& var name=TestJson.cityN&&
&&&&&&&&&&
&&&&&&&&&&& html+="&option value="+id+"&"+name+"&/option&";&&&&&&&
&&&&& $("#city option").remove();
&&&&&&&&&&&&&&&&& $("#city").append(html);&
function changeCity(){
&&&&& $.ajax({
type: "post",
url : 'AjxServlet',
data: { city:$('#city').val(),province:$('#province').val()},
success : function(result){
if(eval(result)) {
var html="&option value='0' &--请选择--&/option&";
for(var i=2;i&result.i++){
var TestJson =result[i];
&&&&&&&&&&& var id=TestJson.countyNo;
&&&&&&&&&&& var countName=TestJson.countyN&&&&
&&&&&&&&&&& html+="&option value="+id+"&"+countName+"&/option&";&&&&&&&
&&&&& $("#county option").remove();
&&&&&&&&&&&&&&&&& $("#county").append(html);&
};
&/script&
&/head&
&body&
&form id="form1" name="form1" method="post" action=""&
& &table width="542" height="168" border="1" cellpadding="0" cellspacing="0"&
&&& &tr&
&&&&& &td height="46" colspan="3"&&div align="center"&级联ajax+jquery+json测试&/div&&/td&
&&& &/tr&
&&& &tr&
&&&&& &td width="170" height="56"&省份:
&&&&&&& &label&
&&&&&&& &select name="select" id="province" onchange="sub()"&
&&&&&&& &option value='0' &--请选择--&/option&
&&&&&&& &option value="1"&湖南省&/option&
&&&&&&&& &option value="2"&湖北省&/option&
&&&&&&&&& &option value="3"&广东省&/option&
&&&&&&&&&& &option value="4"&海南省&/option&
&&&&&&& &/select&
&&&&& &/label&&/td&
&&&&& &td width="178"&城市:
&&&&&&& &label&
&&&&&&& &select name="select2" id="city" onchange="changeCity()"&
&&&&&&&& &option value='0' &----&/option&
&&&&&&& &/select&
&&&&& &/label&&/td&
&&&&& &td width="186"&县城:
&&&&&&& &label&
&&&&&&& &select name="select3" id="county"&
&&&&&&&& &option value='0' &----&/option&
&&&&&&& &/select&
&&&&& &/label&&/td&
&&& &/tr&
&&& &tr&
&&&&& &td height="56"&&&/td&
&&&&& &td&&&/td&
&&&&& &td&&&/td&
&&& &/tr&
& &/table&
&/form&
&/body&
&/html&
web.xml文件添加以下代码
&servlet&
&&& &servlet-name&AjxServlet&/servlet-name&
&&& &servlet-class&com.zz.jquery.AjxServlet&/servlet-class&
& &/servlet&
& &servlet-mapping&
&&& &servlet-name&AjxServlet&/servlet-name&
&&& &url-pattern&/AjxServlet&/url-pattern&
& &/servlet-mapping&
(2) 使用JSONArray json=JSONArray.fromobject(list);在调用json.toString()方法转换成字符串
只需要将上面后台代码的 Gson gson = new Gson();
String reslut = gson.toJson(list);
换成
JSONArray json=JSONArray.fromobject(list);
前提是list必须都为一个完整的对象
所需jar包:
json_java.jar
gson-1.6.jar
具体的项目源码请在附件中下载。
(512.6 KB)
下载次数: 148
浏览 37223
zhangzhi199129
浏览: 113594 次
来自: 广州
你这标题中说的内容和标题不相关内容也太不成比例了,重点内容整成 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'js如何把json格式的字符串转成成json? - 知乎23被浏览<strong class="NumberBoard-itemValue" title="分享邀请回答json.org22 条评论分享收藏感谢收起03 条评论分享收藏感谢收起博客分类:
JSON是JavaScript面向对象语法的一个子集。由于JSON是JavaScript的一个子集,因此它可清晰的运用于此语言中。
& 直接在Javascript中创建一个Json数据对象的写法:
&
&& var myJson={name:'Cloud.L',homepage:'http://www.cnode.cn'};
&& //调用其中的属性
& alert(myJson.name);
&&
&& 而对于JSON字符串就需要做一个处理,避免语法上的歧义。文本必须括在括号中避免产生JavaScript的语法歧义。
&
&& var myTest="{name:'Cloud.L',homepage:'http://www.cnode.cn'}",
&& oTest=eval('('+myTest+')');
&& alert(oTest.name+","+oTest.homepage);
浏览: 117112 次
来自: 深圳
&value&0 0 24 ? * SAT& ...
请问一下 每周6
时间怎么设置呀
parseInt(string) 隐式的转换
parseInt ...
这个有安全性问题吧,貌似IE浏览器要把某项勾选才能用,还是写J ...
不是我想要的
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 js将字符串转化为变量 的文章

 

随机推荐