服务端传回的数据是这样的前台怎么转换成json格式便利数据啊?
<string>{"img":[{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"}]}</string>
有一个问题就是你的这个{}里面都是FileUpName,SavePath,对象的属性名不能重复的
var str = "json字符串",
JSON.parse(str.replace(/\'/g,'\"').substring(str.indexOf("{"),str.indexOf("}")+1))
你可以试一下
<string>{"img":[{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"}]}</string>
那这样的格式呢? 问题的格式是我自己写的。这些事生成的,但是在前台也没法处理
@Like金: 这个是可以的
@Like金: JSON.parse(str.substring(str.indexOf("{"),str.lastIndexOf("}")+1))
测试通过
@codingHeart: TypeError: str.replace is not a function 提示不是一个函数,是不是得引用什么啊?
如图:
@Like金: 给定的json字符串加上单引号‘’ var str = ‘<string>{"img":[{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"}]}</string>’
@Like金: 你发的新json字符串格式不需要replace函数了
var str = ‘<string>{"img":[{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"}]}</string>’
var result = JSON.parse(str.substring(str.indexOf("{"),str.lastIndexOf("}")+1))
我这边能通过 处理方式应该没问题的
@codingHeart:
$.ajax({
type: "get",
//contentType: "application/json;charset=utf-8", //WebService 会返回Json类型
url: "http://localhost:54753/WebService1.asmx/FileShowListString", //调用WebService
//dataType: 'string',
headers: { "Access-Control-Allow-Origin": "*" },
data: { id: 1 },
success: function (result) {
var str = result;
//JSON.parse(str.substring(str.indexOf("{"), result.lastIndexOf("}") + 1));
alert(JSON.parse(str.substring(str.IndexOf("{"), str.lastIndexOf("}") + 1)));
},
error: function (err, status) {
alert("请求失败!");
}
});
@codingHeart:
@codingHeart:
@codingHeart: 还是不行啊
@Like金: 看着没问题啊 var str = “‘”+ result+“’”;这个地方试一下吧
@Like金: 小写的indexOf()啊
@Like金: json那段函数直接copy我的吧.....
@codingHeart: 就是copy你的啊但是还是不行。TypeError: str.indexOf is not a function
这个错误。
@Like金: 吃饭去了
var str = “‘”+ result+“’”;这条语句换了没的,indexOf是String的函数,不会没有的
@Like金:
把这个代码跑一下,给我看下结果,WebService可以返回json格式的数据的,你那个返回的是xml
$.ajax({ type: "POST", //contentType: "application/json;charset=utf-8", //WebService 会返回Json类型 url: "http://localhost:54753/WebService1.asmx/FileShowListString", //调用WebService dataType: 'json', //headers: { "Access-Control-Allow-Origin": "*" }, data: { id: 1 }, success: function (result) { //var str = “‘”+ result+“’”; //JSON.parse(str.substring(str.indexOf("{"), result.lastIndexOf("}") + 1)); //alert(JSON.parse(str.substring(str.IndexOf("{"), str.lastIndexOf("}") + 1))); console.log(result); }, error: function (err, status) { alert("请求失败!"); } });
@codingHeart: 这样就提示请求失败了,
public string FileShowListString(int id)
{
Context.Response.AddHeader("Access-Control-Allow-Origin", "*");
List<object> list = new List<object>();
StringBuilder str = new StringBuilder();
str.Append("select * from FileUptab where UserId=" + id);
DataTable dt = DBhelper.GetTable(str.ToString());
FileUpLoad img = new FileUpLoad();
foreach (DataRow dr in dt.Rows)
{
img.FileUpName = dr["FileUpName"].ToString();
img.SavePath = dr["SavePath"].ToString();
list.Add(img);
}
ObjectListToJSONcs t = new ObjectListToJSONcs();
string json = t.toJSON(list,"img");
return json.ToString();
}
这是我web service
@Like金: 最后一句(return json.ToString();)换成Context.Response.Write(json.ToString());应该就可以了
@codingHeart: 反不回去
@Like金: 后台代码不能改?
@codingHeart:
@Like金: 函数定义的string 换成void,你这得上点心啊,兄弟
@codingHeart:
不是我不上心啊 这没法返回啊
@Like金: 不要return.................
@codingHeart: .... 还是不行 不要return 就没有数据了,还报数据库连接错误。
@Like金:
public void FileShowListString(int id) { Context.Response.AddHeader("Access-Control-Allow-Origin", "*"); List<object> list = new List<object>(); StringBuilder str = new StringBuilder(); str.Append("select * from FileUptab where UserId=" + id); DataTable dt = DBhelper.GetTable(str.ToString()); FileUpLoad img = new FileUpLoad(); foreach (DataRow dr in dt.Rows) { img.FileUpName = dr["FileUpName"].ToString(); img.SavePath = dr["SavePath"].ToString(); list.Add(img); } ObjectListToJSONcs t = new ObjectListToJSONcs(); string json = t.toJSON(list,"img"); Context.Response.Write(json.ToString());
Context.Response.End(); }
这应该不干数据库的事儿啊
@codingHeart: 好啦。可以了,刚才数据库服务自动给关了,尴尬。。。。。我才发现。
谢谢你啦,奖励给你翻倍。么么哒。
@Like金:
感动死我了,也不好意思,能改后台的话,第一时间应该让你改后台的,折腾这么久。
@codingHeart: 哈哈,还是谢谢你,帮我折腾这么长时间。
JSON.parse(str)
这样不行 格式不对,
@Like金: 要用双引号,单引号不行
@by.Genesis: 还是不行 报格式错误
<string>{"img":[{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"}]}</string>
这样的格式该怎么处理啊?这是list集合的数据转成了json字符串传到前台的,前台怎么给他转成json我取数据用
@Like金: 先把 <string> 和 </string> 删掉:
str = str.replace(/<\/?string>/g, '')
然后再 JSON.parse(str)
参考一下 汤姆大叔的 JSON 理解
http://www.cnblogs.com/TomXu/archive/2012/01/11/2311956.html
这格式真变态,你最好和后端约定好,要么都标准json,要么xml。你这又有xml,又像json的算什么东东。。
另外,如果实在是没办法,你就先把数据处理一次,然后在转换为json对象(去除无效内容,替换单引号)。
我用的web service返回的json字符串,本来是这样的list集合便利的,<string>{"img":[{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg","SaveTime":"","UserId":"0"}]}</string>
上面的是我更改过的格式,但是前台没法处理成json了
@Like金: 很好处理嘛。直接把前后的string标签干掉,然后JSON.parse(str)即可。
@幻天芒: 关键是干不掉啊....能给我段代码吗?
@Like金: 字符串处理,怎么就干不掉了?
破除幻想,不要在错误的道路上越走越远,你又想用上熟悉的webservice又想使用json的便利,这种后果你也知道了。不要一条路走到黑,全换成json不就可以了么?如果这是别人提的就怼回去,什么破接口,脑洞不要开的太大。怼不回去那就没办法了,上正则咬吧。
在www.bejson.com里面验证下你的json字符串是否符合格式 然后在定义类 反序列化成对象 或者集合
ka看了那么多..都没办<string>删掉...
这么快就有满意答案了。不过还是看看我的:
针对于你给的这个字符串本身,要转成json对象首先得去掉首尾的<string>和</string>,然后就是你json字符串中的路径那一项:
"SavePath":"C:\Users\yangjinlei\Desktop\image\A00003.jpg",这个本身这样转的话是不通过的,得先把反斜杠转换成正斜杠就不会有问题。
最后就可以直接调jQuery.parseJSON()转了。
$(function () { var jsonStr = $("input[type=text]").val();//我这里是把字符串装在了文本框中 jsonStr = jsonStr.replace("<string>", "").replace("</string>", "").replace(/\\/g,"/"); $("#js").text(jsonStr); var json = jQuery.parseJSON(jsonStr); alert(json.img.length); })
如果能弹出数字,证明装成功了。
{"img":[{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:/Users/yangjinlei/Desktop/image/A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:/Users/yangjinlei/Desktop/image/A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:/Users/yangjinlei/Desktop/image/A00003.jpg","SaveTime":"","UserId":"0"},{"ID":"0","FileUpName":"A00003.jpg","UpdateFileUpName":"","UpLoadPath":"","SavePath":"C:/Users/yangjinlei/Desktop/image/A00003.jpg","SaveTime":"","UserId":"0"}]}
最后这一段代码是转成功后的代码
还是要谢谢你,全是经验,谢了兄弟。