2 u010357690 u010357690 于 2014.12.08 17:00 提问

webservice写的json格式客户端无法读出

网上看到很多解答,但我没搞定。
下面是我写的带代码
封装函数
public DataSet js()
{
string sql = "select * from friend";
SqlCommand cmd = new SqlCommand(sql, con);
//SqlDataReader da = cmd.ExecuteReader();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.Dispose();
return ds;
}
转化数据
public string js()
{
StringBuilder sb = new StringBuilder();

    DataTable dt = dbOperation.js().Tables[0];
    sb.Append("{User:[");
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        sb.Append("{\"userid\":\"" + dt.Rows[i]["userid"].ToString() + "\",\"name\":\"" + dt.Rows[i]["name"].ToString() + "\",\"password\":\"" + dt.Rows[i]["password"].ToString() + "\",\"text\":\"" + dt.Rows[i]["text"].ToString() + "\"}");
        if (i != dt.Rows.Count - 1)
        {
            sb.Append(",");
        }
    }
    sb.Append("]}");
    return sb.ToString();

}

运行效果
<?xml version="1.0" encoding="UTF-8"?>
{User:[{"userid":"123456","name":"吉旺","password":"123456 ","text":"asd"},{"userid":"41238","name":"华小坑","password":"123456 ","text":""}]}
客户端读取
var Service=function() {
Service.initializeBase(this);
this._timeout = 0;
this._userContext = null;
this._succeeded = null;
this._failed = null;
}
Service.prototype={
_get_path:function() {
var p = this.get_path();
if (p) return p;
else return Service._staticInstance.get_path();},
HelloWorld:function(succeededCallback, failedCallback, userContext) {
return this._invoke(this._get_path(), 'HelloWorld',false,{},succeededCallback,failedCallback,userContext); },
selectAllUser:function(succeededCallback, failedCallback, userContext) {
return this._invoke(this._get_path(), 'selectAllUser',false,{},succeededCallback,failedCallback,userContext); },
insertUser:function(name,pwd,succeededCallback, failedCallback, userContext) 麻烦帮助一下

4个回答

u010357690
u010357690   2014.12.25 15:39
已采纳

用WebService只能返回一个json格式的字符串

showbo
showbo   Ds   Rxr 2014.12.08 17:18

你返回的内容怎么增加了xml申明?这种不是json格式的字符串了,你客户端当然会解析出错。自己改正这个错误来~

showbo
showbo 回复u010357690: 运行效果 <?xml version="1.0" encoding="UTF-8"?> 多了这个xml申明了
3 年多之前 回复
u010357690
u010357690 我的json转换有问题?
3 年多之前 回复
oyljerry
oyljerry   Ds   Rxr 2014.12.08 19:05

数据格式不是json格式吧
这下面才是json格式
{User:[{"userid":"123456","name":"吉旺","password":"123456 ","text":"asd"},{"userid":"41238","name":"华小坑","password":"123456 ","text":""}]}

oyljerry
oyljerry 有很多在线json格式化检测网站,检查一下语法
3 年多之前 回复
u010357690
u010357690 客户端读不出来
3 年多之前 回复
u010357690
u010357690 我的问题是酷虎端读不出来 我的json格式有问题?
3 年多之前 回复
u010357690
u010357690   2014.12.09 13:17

我现在的返回值是这个,用webservice写的,可能只能返回一个json字符串,可以吗

{User:[{"userid":"123456","name":"吉旺","password":"123456 ","text":"asd"},{"userid":"41238","name":"华小坑","password":"123456 ","text":""},{"userid":"","name":"qwe","password":"346 ","text":"a12341231230111fghhjhhddddghhgffgggdghhgffgggdghhgffgggdfsdfsefdsadsdsddoujianyuqwertqwertjojiojojiojoijoijio88888888"},{"userid":"","name":"doujianyu","password":"123 ","text":""},{"userid":"","name":"窦建雨","password":"123 ","text":""},{"userid":"","name":"都教育","password":"123 ","text":""},{"userid":"","name":"对公司的规划和闪光灯和身高多少个电话说过话","password":"string ","text":""},{"userid":"","name":"对公司的规划和闪光灯和身高多少个电话说过话大二上大声的撒是滴是滴","password":"string ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"rty","password":"sdf ","text":"asd"},{"userid":"","name":"对公司的规划和闪光灯和身高多少个电话说过话大二上大声的撒是滴是滴","password":"string ","text":""},{"userid":"","name":"对公司的规划和闪光灯和身高多少个电话说过话大二上大声的撒是滴是滴","password":"string ","text":""},{"userid":"","name":"稳定完善的完善的感受到官网上高度计划前往该等哈说几个大叔跟大家","password":"string ","text":""},{"userid":"","name":"稳定完善的完善的感受到官网上高度计划前往该等哈说几个大叔跟大家的速度速度速度速度","password":"string ","text":""},{"userid":"","name":"稳定完善的完善的感受到官网上高度计划前往该等哈说几个大叔跟大家的速度速度速度速度打发打发打发","password":"string ","text":""},{"userid":"","name":"稳定完善的完善的感受到官网上高度计划前往该等哈说几个大叔跟大家的速度速度速度速度打发打发打发大法","password":"string ","text":""},{"userid":"","name":"稳定完善的完善的感受到官网上高度计划前往该等哈说几个大叔跟大家的速度速度速度速度打发打发打发大法的","password":"string ","text":""},{"userid":"","name":"稳定完善的完善的感受到官网上高度计划前往该等哈说几个大叔跟大家的速度速度速度速度打发打发打发大法的的","password":"string ","text":""},{"userid":"","name":"稳定完善的完善的感受到官网上高度计划前往该等哈说几个大叔跟大家的速度速度速度速度打发打发打发大法的的","password":"string ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是这视频播放日物流啥是一二三是物流七八就是","password":"123 ","text":""},{"userid":"","name":"窦建雨","password":"123 ","text":""},{"userid":"","name":"窦建雨","password":"123456 ","text":""},{"userid":"","name":"管理办法莫基金经理了了内燃机车恶魔软件开发忙忙碌碌家软件开发才管理理念人口密度打基金经理尽可能带获","password":"123 ","text":""},{"userid":"","name":"名单的软件开发哪里几个解决方法理解咯的","password":"123 ","text":""},{"userid":"","name":"qwe","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是而抛弃物流七八就是一二三四物流七八就是一二三四物流七八就是一人抛弃物流七八就是","password":"123 ","text":""},{"userid":"","name":"二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是而抛弃物流七八就是一二三四物流七八就是一二三四物流七八就是一人抛弃物流七八就是","password":"123 ","text":""},{"userid":"","name":"二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是","password":"123 ","text":""},{"userid":"","name":"一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是一二三四物流七八就是","password":"123 ","text":""},{"userid":"","name":"空咯莫默默呢","password":"123 ","text":""},{"userid":"","name":"最近几年吧里克尔梅","password":"123 ","text":""},{"userid":"","name":"最近几年吧里克尔梅","password":"123 ","text":""},{"userid":"","name":"他咯莫","password":"123 ","text":""},{"userid":"","name":"尽量避免经济实力咯","password":"123 ","text":""},{"userid":"","name":"公开透明公积金管理昆明即可基金管理咯比较什么时间虐托咯少年时代头多了你灭了肉哦市场前景而后多楼不吧啊","password":"123 ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"vwoijgwe213213","password":"fwr2132314","text":""},{"userid":"","name":"vwoijgwe213213","password":"fwr2132314","text":""},{"userid":"","name":"vwoijgwe213213","password":"fwr2132314","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":" ","text":""},{"userid":"","name":"","password":"","text":""}]}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!