首页 新闻 搜索 专区 学院

json字符串转json

1
悬赏园豆:50 [已解决问题] 解决于 2017-03-31 13:59

服务端传回的数据是这样的前台怎么转换成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>

Like金的主页 Like金 | 初学一级 | 园豆:42
提问于:2017-03-31 10:56
< >
分享
最佳答案
0

有一个问题就是你的这个{}里面都是FileUpName,SavePath,对象的属性名不能重复的

 

var str = "json字符串",

JSON.parse(str.replace(/\'/g,'\"').substring(str.indexOf("{"),str.indexOf("}")+1))

 

你可以试一下

收获园豆:50
codingHeart | 小虾三级 |园豆:1511 | 2017-03-31 11:16

<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金 | 园豆:42 (初学一级) | 2017-03-31 11:18

@Like金: 这个是可以的

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 11:20

@Like金: JSON.parse(str.substring(str.indexOf("{"),str.lastIndexOf("}")+1))

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 11:30

 测试通过

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 11:31

@codingHeart: TypeError: str.replace is not a function   提示不是一个函数,是不是得引用什么啊?

Like金 | 园豆:42 (初学一级) | 2017-03-31 11:33

如图:

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 11:34

@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>’

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 11:37

@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 | 园豆:1511 (小虾三级) | 2017-03-31 11:40

@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("请求失败!");
}
});

 

Like金 | 园豆:42 (初学一级) | 2017-03-31 11:49

@codingHeart: 

Like金 | 园豆:42 (初学一级) | 2017-03-31 11:49

@codingHeart: 

Like金 | 园豆:42 (初学一级) | 2017-03-31 11:49

@codingHeart: 还是不行啊

Like金 | 园豆:42 (初学一级) | 2017-03-31 11:50

@Like金:  看着没问题啊  var str = “‘”+ result+“’”;这个地方试一下吧

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 11:51

@Like金: 小写的indexOf()啊

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 11:53

@Like金: json那段函数直接copy我的吧.....

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 11:54

@codingHeart: 就是copy你的啊但是还是不行。TypeError: str.indexOf is not a function

这个错误。

Like金 | 园豆:42 (初学一级) | 2017-03-31 11:58

@Like金: 吃饭去了

var str = “‘”+ result+“’”;这条语句换了没的,indexOf是String的函数,不会没有的

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 12:26

@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 | 园豆:1511 (小虾三级) | 2017-03-31 12:34

@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金 | 园豆:42 (初学一级) | 2017-03-31 13:31

@Like金:  最后一句(return json.ToString();)换成Context.Response.Write(json.ToString());应该就可以了

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 13:33

@codingHeart:   反不回去

Like金 | 园豆:42 (初学一级) | 2017-03-31 13:35

@Like金: 后台代码不能改?

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 13:37

@codingHeart: 

Like金 | 园豆:42 (初学一级) | 2017-03-31 13:37

@Like金: 函数定义的string 换成void,你这得上点心啊,兄弟

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 13:38

@codingHeart: 

 

 

不是我不上心啊  这没法返回啊

Like金 | 园豆:42 (初学一级) | 2017-03-31 13:40

@Like金: 不要return.................

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 13:42

@codingHeart: .... 还是不行 不要return 就没有数据了,还报数据库连接错误。

Like金 | 园豆:42 (初学一级) | 2017-03-31 13:48

@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 | 园豆:1511 (小虾三级) | 2017-03-31 13:55

@codingHeart: 好啦。可以了,刚才数据库服务自动给关了,尴尬。。。。。我才发现。

 

 

谢谢你啦,奖励给你翻倍。么么哒。

Like金 | 园豆:42 (初学一级) | 2017-03-31 13:58

@Like金: 

感动死我了,也不好意思,能改后台的话,第一时间应该让你改后台的,折腾这么久。

codingHeart | 园豆:1511 (小虾三级) | 2017-03-31 14:00

@codingHeart: 哈哈,还是谢谢你,帮我折腾这么长时间。

Like金 | 园豆:42 (初学一级) | 2017-03-31 14:01
其他回答(8)
0

JSON.parse(str)

by.Genesis | 园豆:1911 (小虾三级) | 2017-03-31 11:03

这样不行  格式不对,

支持(0) 反对(0) Like金 | 园豆:42 (初学一级) | 2017-03-31 11:04

@Like金: 要用双引号,单引号不行

支持(0) 反对(0) by.Genesis | 园豆:1911 (小虾三级) | 2017-03-31 11:06

@by.Genesis: 还是不行 报格式错误

支持(0) 反对(0) Like金 | 园豆:42 (初学一级) | 2017-03-31 11:12

<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我取数据用

支持(0) 反对(0) Like金 | 园豆:42 (初学一级) | 2017-03-31 11:19

@Like金: 先把 <string> 和 </string> 删掉:

str = str.replace(/<\/?string>/g, '')

然后再 JSON.parse(str)

支持(0) 反对(0) by.Genesis | 园豆:1911 (小虾三级) | 2017-03-31 11:33
0
Ctrl` | 园豆:2555 (老鸟四级) | 2017-03-31 11:04
0

参考一下 汤姆大叔的 JSON 理解

http://www.cnblogs.com/TomXu/archive/2012/01/11/2311956.html

流年过往 | 园豆:464 (菜鸟二级) | 2017-03-31 11:05
0

这格式真变态,你最好和后端约定好,要么都标准json,要么xml。你这又有xml,又像json的算什么东东。。

 

另外,如果实在是没办法,你就先把数据处理一次,然后在转换为json对象(去除无效内容,替换单引号)。

幻天芒 | 园豆:36608 (高人七级) | 2017-03-31 11:08

我用的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了

支持(0) 反对(0) Like金 | 园豆:42 (初学一级) | 2017-03-31 11:17

@Like金: 很好处理嘛。直接把前后的string标签干掉,然后JSON.parse(str)即可。

支持(0) 反对(0) 幻天芒 | 园豆:36608 (高人七级) | 2017-03-31 11:21

@幻天芒: 关键是干不掉啊....能给我段代码吗?

支持(0) 反对(0) Like金 | 园豆:42 (初学一级) | 2017-03-31 11:22

@Like金: 字符串处理,怎么就干不掉了?

支持(0) 反对(0) 幻天芒 | 园豆:36608 (高人七级) | 2017-03-31 13:17
0

破除幻想,不要在错误的道路上越走越远,你又想用上熟悉的webservice又想使用json的便利,这种后果你也知道了。不要一条路走到黑,全换成json不就可以了么?如果这是别人提的就怼回去,什么破接口,脑洞不要开的太大。怼不回去那就没办法了,上正则咬吧。

Daniel Cai | 园豆:10374 (专家六级) | 2017-03-31 11:26
0

在www.bejson.com里面验证下你的json字符串是否符合格式  然后在定义类 反序列化成对象 或者集合

塞下一群鹅 | 园豆:25 (初学一级) | 2017-03-31 11:34
0

ka看了那么多..都没办<string>删掉...

吴瑞祥 | 园豆:28841 (高人七级) | 2017-03-31 13:50
0

这么快就有满意答案了。不过还是看看我的:

针对于你给的这个字符串本身,要转成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"}]}

最后这一段代码是转成功后的代码

龙行天涯 | 园豆:1821 (小虾三级) | 2017-03-31 14:41

还是要谢谢你,全是经验,谢了兄弟。

支持(0) 反对(0) Like金 | 园豆:42 (初学一级) | 2017-03-31 14:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册