日期:2014-05-17  浏览次数:20679 次

解析json格式数据,要求有点难。。。。。
本帖最后由 u010212112 于 2013-04-15 17:49:16 编辑
var str={"act":"{\"act\":\"inittemplate\",\"titleid\":\"2\"}","newName":null,"obj":[{"titleid":2,"titlename":"医疗产品","tsid":1,"tsname":"自动注射器","tssid":1,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BYZ-810"},{"titleid":2,"titlename":"医疗产品","tsid":1,"tsname":"自动注射器","tssid":2,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"史密斯输液器"},{"titleid":2,"titlename":"医疗产品","tsid":2,"tsname":"血压器","tssid":3,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BYZ-810"},{"titleid":2,"titlename":"医疗产品","tsid":2,"tsname":"血压器","tssid":4,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BYZ-810"},{"titleid":2,"titlename":"医疗产品","tsid":3,"tsname":"血糖仪","tssid":5,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BYZ-810"},{"titleid":2,"titlename":"医疗产品","tsid":3,"tsname":"血糖仪","tssid":6,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BYZ-810"}]}

上面的是json格式数据

var data2 = eval("(" + str + ")");
var jsonstring2 = data2.obj;
var tempids = "";
for (i in jsonstring2) {
                        jsonstring2[i].tsname//这个地方等得到值,
}
需求是:  要把json解析成
"tsid":2 "tsname":"血压器"
      "tssid":4   "tssname":"比杨单通道注射泵BYZ-810"
      "tssid":3   "tssname":"比杨单通道注射泵BYZ-810"
"tsid":1,"tsname":"自动注射“
     "tssid":2  "tssname":"史密斯输液器"
     "tssid":1  "tssname":"比杨单通道注射泵BYZ-810"

怎么把得到的结果转换成这个样子,请大侠给出代码···········
       

------解决方案--------------------
我想问下楼主,你这是前台数据,干嘛还要这样取
如果是从后台传过来的数据的话,就用ajax的Json传值,这样好解析些


你可以像如下方法取得你需要的几个值

var obj = msg.obj;
for(int i=0;i<obj.length,i++){
var titleid = obj[i].titleid;
var titlename = obj[i].titleid;
...
}

------解决方案--------------------
引用:
引用:你在append的时候先判断改tsname在<h2>标签中是否已经存在 存在则将该tssname apend到该tsname下面  
否则则新创建一个<h2>标签给个代码,我取不出来

效果图

代码

  <script type="text/javascript">
   $(function(){
   //$('#tt').tree({
   // url:'EnergyAction!doTree.action'
   //});
   var str={"act":"{\"act\":\"inittemplate\",\"titleid\":\"2\"}","newName":null,"obj":[{"titleid":2,"titlename":"医疗产品","tsid":1,"tsname":"自动注射器","tssid":1,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BYZ-810"},{"titleid":2,"titlename":"医疗产品","tsid":1,"tsname":"自动注射器","tssid":2,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"史密斯输液器"},{"titleid":2,"titlename":"医疗产品","tsid":2,"tsname":"血压器","tssid":3,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BYZ-810"},{"titleid":2,"titlename":"医疗产品","tsid":2,"tsname":"血压器","tssid":4,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BYZ-810"},{"titleid":2,"titlename":"医疗产品","tsid":3,"tsname":"血糖仪","tssid":5,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BYZ-810"},{"titleid":2,"titlename":"医疗产品","tsid":3,"tsname":"血糖仪","tssid":6,"tssimg":"\/cy\/images\/byz01.jpg","tssname":"比杨单通道注射泵BY