本文目錄一覽:
- 1、求asp編輯JSON數據文件的類源碼
- 2、ThinkPHP 生成下面格式json!求大俠給源碼!
- 3、用原始方法解析複雜字符串,json一定要用JsonMapper么
- 4、純lua判斷字符串是否是規範的json格式
求asp編輯JSON數據文件的類源碼
asp支持jscript 所以js寫的代碼在asp里大部分都能運行
%’默認vbscript,可以用jscript定義的函數變量
str=”{}”‘假設你把文件讀出來了,讀文件很簡單的
set obj=evalJs(“(“+str+”)”)’解析json
addJs obj,”value”,”abcd”‘添加內容
addJs obj,”value”,true’修改value=true
evalJs “obj.obj={key1:true,key2:[]}”‘添加對象 強大的asp多語言支持
delete obj.obj,”key1″‘刪除key1
evalJs(“delete obj.obj”)’刪除obj
delete obj,”value”‘刪除value
str=JSON.stringify(obj)’生成json串,這裡的JSON.stringify是從json官網下的json2.js文件中的方法,把json2.js下過來把代碼插到下面。
‘json2.js下載地址底下 javascript json2.js
‘….保存str到文件,很簡單的
%
script runat=”server” language=”jscript”//服務器運行jscript,代碼無論放哪裡都比默認vbscript%%內腳本先執行,如果language=”vbscript”就在%%內腳本後執行
//下載的json2.js代碼插到這裡
function evalJs(json){//轉換對象
return eval(json);
}
function addJs(obj,key,value){//添加修改
obj[key]=value;
}
function delJs(obj,key){//刪除
delete obj[key];
}
/script
ThinkPHP 生成下面格式json!求大俠給源碼!
$a = mysql_query(“select * from 表”);
json_encode($a);
//輸出看看
print_r($a);
用原始方法解析複雜字符串,json一定要用JsonMapper么
1.不規則非json字符串
先看看這個例子,字符串是連在一起,沒有換行的,為了方便觀察,換行了,程序是原始在一起的:
[11101630,1532,14,’0′,’0′,3,’2015,4,23,16,05,48′,’4′,1,2,0,0],
[11101631,1532,14,’0′,’0′,3,’2015,4,23,16,09,48′,’0′,,,0,0],
[11101632,1532,14,’0′,’0′,3,’2015,4,23,16,03,10′,’1′,2,2,0,0]
先來分析一下這個字符串的特點,才能找到思路哦:
1.每一組數據都是在[]括號對中,每一組數據用,號分割,所以最終要形成一個數組來訪問哦。
2.每一組的數據基本都是用 , 號分割,字符串類型還有單引號 ;
3.第7個數組是一個整體,也使用,號分割,整體是字符串有引號;
4.第2組數據有空值,直接用,號分割,所以splite的時候不能去掉空值,否則數組長度不一樣,定位就亂了。
既然分析都完了,那思路呢?
1.組直接分割使用 ], 標記,然後每一組要Repalce掉 [ 和 ] 。主要是最前和最後;
2.組內分割,使用 ,號標記分割,出來之前要把單引號給 替換掉 ;不然也是作為字符串,引號也包括進去了;
3.至於那個 數組 的處理,不能過於想複雜,分割之後,直接在最後增加1個元素,將固定位置7-12的組合起來;這樣也許方便點;
4.由於空值有佔位,所以每一組的長度是固定的。所以處理的時候直接根據自己想要的位置來組合。
下面看看代碼了,C#版本,相對與一行代碼,仔細看,Linq很是一個神器,真的是神奇。。。說多了都是淚,為啥就沒早點學呢:
String str = @”[11101630,1532,14,’0′,’0′,3,’2015,4,23,16,05,48′,’4′,1,2,0,0],[11101631,1532,14,’0′,’0′,3,’2015,4,23,16,09,48′,’0′,,,0,0],[11101632,1532,14,’0′,’0′,3,’2015,4,23,16,03,10′,’1′,2,2,0,0]”;
var result = str.Split(new string[] { “],” }, StringSplitOptions.None) //先整體分割組
.Select(n = n.Replace(“[“, “”) //以下是組內分割,並去掉其他干擾字符
.Replace(“]”, “”)
.Replace(“\'”, “”)
.Split(‘,’).ToList())
.Select(n = //對中間一個整體單獨提取,進行組合,單獨增加一個元素
{
n.Add(String.Format(“{0},{1},{2},{3},{4},{5}”, n[6], n[7], n[8], n[9], n[10], n[11]));return n;
}).ToList();
看看結果怎麼樣:
2.鍵值對字符串分割函數
由於json數據格式都是鍵值對字符串,所以這裡特意分享一個經常用到的分割函數,不用Json組件,那就用簡單的方法做一個。這個函數來源於 Newlife.Core ,是 X組件 的重要部分。源碼部分不過多解釋,就是按規則將鍵值對直接分割保持在字典中,使用方法大家可以自己實驗一下,或者參考下面的案例,都有用到這個方法。代碼如下,為了方便使用,寫成了擴展方法:
public static class StringHelper
{
/// summary拆分字符串成為名值字典/summary
/// param name=”str”要分割字符串的/param
/// param name=”nameValueSeparator”鍵值對的分隔符/param
/// param name=”separators”分割字符/param
/// returns鍵值對字典/returns
public static IDictionaryString, String SplitAsDictionary(this String str, String nameValueSeparator = “=”, params String[] separators)
{
var dic = new DictionaryString, String();
if (String.IsNullOrWhiteSpace(str)) return dic;
if (String.IsNullOrEmpty(nameValueSeparator)) nameValueSeparator = “=”;
if (separators == null || separators.Length 1) separators = new String[] { “,”, “;” };
String[] ss = str.Split(separators, StringSplitOptions.RemoveEmptyEntries);
if (ss == null || ss.Length 1) return null;
foreach (var item in ss)
{
Int32 p = item.IndexOf(nameValueSeparator);
// 在前後都不行
if (p = 0 || p = item.Length – 1) continue;
String key = item.Substring(0, p).Trim();
dic[key] = item.Substring(p + nameValueSeparator.Length).Trim();
}
return dic;
}
}
上面默認的鍵值對分割符號為 = 號,根據實際情況進行修改,json格式裡面一般是:冒號比較多。
3.複雜Json格式的字符串
上面的例子比較簡單,這次看一個稍微複雜點的,雖然可能用JsonMapper可以很輕易做到,但試一下最原始的方法吧。還是按照上面的思路,先分析字符串的特點: 字符串是連在一起,沒有換行的,為了方便觀察,換行了,程序是原始在一起的:
{1074:[‘墨聯’,’墨聯’,’MEX D1′,’#098000′,’98’],
2100:[‘美乙’,’美乙’,’USL D2′,’#E89B10′,’98’],
1024:[‘阿甲’,’阿甲’,’ARG’,’#00CCFF’,’98’],
1052:[‘哥倫甲’,’哥倫甲’,’COLCMA’,’#888500′,’98’],
1028:[‘K聯賽’,’K聯賽’,’KORL’,’#F75000′,’98’],
1297:[‘球會友誼’,’球會友誼’,’CF’,’#5691D8′,’98’],
2085:[‘奧女甲’,’奧女甲’,’AFB’,’#D86220′,’97’]}
還是先分析特點,這個格式應該是json類似的了,比較規則:
組與之間是使用 , 號分割;前後有{}括號對;觀察前後可以使用 ], 字符串將組分開;
鍵 是整數,鍵值是通過 : 號分割;
值是一個數組,有5個元素,通過 , 號分割
都有單引號,需要過濾掉;其他沒有特殊情況;
代碼解決過程:
string text = @”{1074:[‘墨聯’,’墨聯’,’MEX D1′,’#098000′,’98’],2100:[‘美乙’,’美乙’,’USL D2′,’#E89B10′,’98’],1024:[‘阿甲’,’阿甲’,’ARG’,’#00CCFF’,’98’],1052:[‘哥倫甲’,’哥倫甲’,’COLCMA’,’#888500′,’98’],1028:[‘K聯賽’,’K聯賽’,’KORL’,’#F75000′,’98’],1297:[‘球會友誼’,’球會友誼’,’CF’,’#5691D8′,’98’],2085:[‘奧女甲’,’奧女甲’,’AFB’,’#D86220′,’97’]}”;
var dic = text.Replace(“\'”, “”).Split(new String[]{“],”}, StringSplitOptions.None) //先組分割
.Select(n = n.Replace(“{“, “”).Replace(“}”, “”) //將組裡面的干擾字符過濾掉
.Replace(“[“, “”).Replace(“]”, “”)
.SplitAsDictionary(“:”, “\””) //鍵值對處理,冒號分隔符
.ToDictionary(t = t.Key, t = t.Value.Split(‘,’)//值再次進行分割,形成數組
)).ToArray();
看看結果如何:
純lua判斷字符串是否是規範的json格式
樓主參考這個:網頁鏈接
最近發現解碼json串時,如果json串本身格式錯誤會造成程序報錯,那當然不能忍,於是斷斷續續抽着間隙來寫寫了一個禮拜終於寫了一個較為完整的純lua腳本來判斷json串。
因為json用的不是很多,剛開始只是了解到這樣的格式: {“a”:“1”,“b”:“2”,“c”:”3″},還有複雜一點的這種:{ “links”: [
{ “a”: “1”, “b”: “2” },
{ “c”: “3”,”d”: “4”
}]},對於這種,一開始我想的處理方式是先找[..] 然後截取出來,再找{…}然後找逗號”,”,截取逗號前部分後後部分,利用到string.gsub判斷這一小段是不是 “a”:”1″這樣的格式,源碼如下,後面還有處理無限嵌套json的源碼:
–判斷是否是json
function ifIsJson(jsonString)
local head
local pos1,pos2
jsonString =jsonString:atrim()
local String1=string.sub(jsonString,1,1) –最外部大括號
local String2 = string.sub(jsonString,#jsonString)
if String1==”{” and String2 ==”}” then
String1=jsonString
jsonString=string.sub(jsonString,2,-2) –去掉最外部括號
pos1,_=string.find(jsonString,”%[“)
if pos1 then
pos2,_ = string.find(jsonString,”%]”)
if pos2 then
head=string.sub(jsonString,2,pos1-1)
local a,b=string.gsub(head,”(\”-)(.-)(\”-):”,””)
if a ==”” and b==1 then
head=string.sub(jsonString,pos1+1,pos2-1)
while true do
if (pos2)==#jsonString then –沒有後續的了
local result= ContinueCheck(head) –傳入 []里的內容
if result then return true else return false end
else –還有
local result= ContinueCheck(head) –傳入 []里的內容
if result== false then return false end
jsonString=string.sub(jsonString,pos2+1,#jsonString) –記錄下後面部分
pos1,_=string.find(jsonString,”%[“)
if pos1 then
pos2,_ = string.find(jsonString,”%]”)
if pos2 then
head=string.sub(jsonString,2,pos1-1)
local a,b=string.gsub(head,”(\”-)(.-)(\”-):”,””)
if a ~=”” and b~=1 then return false end — “head”:[{….},{…..},{…..}] 其中的head格式不正確
head=string.sub(jsonString,pos1+1,pos2-1) –下一次循環傳入的參數
else
return false–缺少]
end
else
return false –[]缺少[]
end
end
end
else
return false — “head”:[{….},{…..},{…..}] 其中的head格式不正確
end
else
return false –不匹配[]
end
else –沒有中括號,簡單的單個{}json處理
local result =ContinueCheck(String1)
if result then return true else return false end
end
else
return false –不匹配{}
end
end
function ContinueCheck(jsonString)
local stringLength=#jsonString
local pos1,pos2=0,0
local JsonTable={}
local i=1
while (true) do –截取{….}並且存入表JsonTable中
pos1,_=string.find(jsonString,”{“,pos1+1)
if pos1 then
pos2,_=string.find(jsonString,”}”,pos2+1)
if pos2 then
JsonTable[i]=string.sub(jsonString,pos1+1,pos2-1)
else
return false
end
else
return false
end
if pos2==#jsonString then break end
i=i+1
end
local a,b
local j=1
while (true) do
jsonString=JsonTable[j] –一個一個值檢查
while (true) do
local q,_=string.find(jsonString,”,”)
if q~= nil then –“a”:”i”,”b”:”j”找這之間的逗號
local jsonString2=string.sub(jsonString,1,q-1) –,號前
jsonString=string.sub(jsonString,q+1,#jsonString) –,號後
a,b=string.gsub(jsonString2,”(\”-)(.-)(\”-):”,””)
else –沒有則為key:value 的最後一個
a,b=string.gsub(jsonString,”(\”-)(.-)(\”-):”,””)
end
if b==1 then
a,b=string.gsub(a,”(\”-)(.+)(\”+)”,””)
if a==”” then
mSleep(10)
else
a=tonumber(a)
if type(a) == “number” then
mSleep(10)
else
return false
end
end
else
return false
end
if q == nil then –找到最後啦
break
end
end
if j==i then return true end
j=j+1
end
end
其中msleep只是封裝的等待,可以看出這樣寫的會嵌套超多層,也就我這種格式規範對的整整齊齊的才看得清,不過勉強作為上述簡單的json判斷還是沒問題的,但是後來考慮到如果json有嵌套,一直嵌套[..]和{..}那要怎麼辦呢,明顯這種從{..}中從前到後截取判斷的方法是不實用的,然後又花了幾天寫了一個可以判斷任意嵌套的封裝函數,有點長還沒做優化但是已經可以正常使用了,代碼如下:
function ifIsJson(JsonString)
local pos1,pos2,pos3,pos4=0,0,0,0
local counter1,counter2,counter3,counter4=0,0,0,0
local string1,string2
local Mytable,Mytable2={},{}
local i,j=1,1
JsonString=JsonString:atrim()
string1=string.sub(JsonString,1,1)
string2=string.sub(JsonString,-1,-1)
if string1==”{” and string2==”}” then –查看各種括號是否成對存在
_,pos1=string.gsub(JsonString,”{“,”{“)
_,pos2=string.gsub(JsonString,”}”,”}”)
_,pos3=string.gsub(JsonString,”%[“,”[“)
_,pos4=string.gsub(JsonString,”%]”,”]”)
if pos1~=pos2 or pos3~=pos4 then return false end
else return false end
while (true) do
pos1,pos2=string.find(JsonString,”,%[{“,pos1)– 找 ,[{ 找到後找 }]
if pos1 then
pos3,pos4=string.find(JsonString,”}]”,pos4)
if pos3 then
string1=string.sub(JsonString,pos1,pos4)
_,counter1=string.gsub(string1,”{“,”{“) –查看各種括號是否成對存在
_,counter2=string.gsub(string1,”}”,”}”)
_,counter3=string.gsub(string1,”%[“,”[“)
_,counter4=string.gsub(string1,”%]”,”]”)
if counter1 == counter2 and counter3== counter4 then
Mytable[i]=string.sub(JsonString,pos2,pos3) –{….}
i=i+1
string1=string.sub(JsonString,1,pos1-1)
string2=string.sub(JsonString,pos4+1)
JsonString=string1..string2 — 去掉,{[..}]
pos4=pos1
end
else return false end
else
pos1,pos2,pos3,pos4=1,1,1,1
pos1,pos2=string.find(JsonString,”%[{“) –找[{ 找到後找 }]沒有則跳出
if pos1 then
pos3,pos4=string.find(JsonString,”}]”)
if pos3 then
string1=string.sub(JsonString,pos1,pos4)
_,counter1=string.gsub(string1,”{“,”{“) –查看各種括號是否成對存在
_,counter2=string.gsub(string1,”}”,”}”)
_,counter3=string.gsub(string1,”%[“,”[“)
_,counter4=string.gsub(string1,”%]”,”]”)
if counter1 == counter2 and counter3== counter4 then
Mytable[i]=string.sub(JsonString,pos2,pos3) –{….}
i=i+1
string1=string.sub(JsonString,1,pos1-1)
string2=string.sub(JsonString,pos4+1)
JsonString=string1..”\”\””..string2 — 去掉,[{..}]
pos4=pos1
end
else return false end
else break end
end
end
i=i-1
if Mytable[i]~= nil then
pos1,pos2,pos3,pos4=1,1,1,1
while (true) do –截取嵌套n層的最裡面的[{…..}]
repeat — 找table[]中[{最靠後的這符號,
pos1,pos2=string.find(Mytable[i],”%[{“,pos2)
if pos1 then pos3,pos4=pos1,pos2 end
until pos1==nil
pos1,pos2=string.find(Mytable[i],”}]”,pos4) –找串中pos4之後}]最靠前的這個符號
if pos1 then
Mytable2[j]=string.sub(Mytable[i],pos4,pos1) –[ {….} ]
j=j+1
string1=string.sub(Mytable[i],1,pos3-1)
stirng2=string.sub(Mytable[i],pos2+1)
Mytable[i]=string1..”\”\””..string2
else
Mytable2[j]=Mytable[i]
j=j+1
i=i-1
if i== 0 then break end–直到找不到成對的[{}]
end
pos2=1
end
end
Mytable2[j]=JsonString
i=1
Mytable={}
pos1,pos2,pos3,pos4=0,0,1,1
while (true) do
repeat
pos1,_=string.find(Mytable2[j],”{“,pos2+1)
if pos1 then pos2=pos1 end
until pos1 == nil
pos3,_=string.find(Mytable2[j],”}”,pos2)
if pos3 and pos2~=1 then
Mytable[i]=string.sub(Mytable2[j],pos2,pos3) — {…}
i=i+1
string1=string.sub(Mytable2[j],1,pos2-1)
string2=string.sub(Mytable2[j],pos3+1)
Mytable2[j]=string1..”\”\””..string2
else
Mytable[i]=string.sub(Mytable2[j],1,pos3)
i=i+1
j=j-1
if j==0 then break end
end
pos2=0
— 串b截取 { “id”:”243125b4-5cf9-4ad9-827b-37698f6b98f0″ } 這樣的格式 存進table[j]
— 剩下一個 “image”:{ “id”:”243125b4-5cf9-4ad9-827b-37698f6b98f0″,”a”:”e0″, “d”:”2431-f6b98f0″,”f”:”243125b98f0″–}這樣的也存進table[j+1]
end
i=i-1
for n=1,i do –去除{}
Mytable[n]=string.sub(Mytable[n],2,-2)
end
while (true) do
pos1,_=string.find(Mytable[i],”,”)
if pos1~= nil then –“a”:”i”,”b”:”j”找這之間的逗號
string1=string.sub(Mytable[i],1,pos1-1)–,前
Mytable[i]=string.sub(Mytable[i],pos1+1)
pos2,_=string.find(string1,”\””)
if pos2==1 then
pos3,pos4=string.find(string1,”\”:”,2)
if pos3 then
string2=string.sub(string1,pos4+1)
else
–(“發現錯誤1”, 1)
return false
end
else
–(“發現錯誤2”, 1)
return false
end
else
pos2,_=string.find(Mytable[i],”\””)
if pos2==1 then
pos3,pos4=string.find(Mytable[i],”\”:”,2)
if pos3 then
string2=string.sub(Mytable[i],pos4+1)
else
–(“發現錯誤3”, 1)
return false
end
else
–(“發現錯誤4”, 1)
return false
end
end
pos2,pos3=string.gsub(string2,”(\”-)(.+)(\”+)”,””)
if pos2==”” or pos2 == “null” then
–(“這一個串格式正確”, 2)
else
pos2=tonumber(pos2)
if type(pos2) == “number” then
–(“這一個串格式正確2”, 2)
else
–(“發現錯誤5”, 1)
return false
end
end
if pos1== nil then
i=i-1
if i==0 then return true end
end
end
end
這裡有一個很核心的思想是既然是無限嵌套都要判斷的出來,那麼腳本要做到的是獲取到最最裡面的{..}單層結構的json,博豬是通過找最後的[ 和{ ,然後在此位置之後找最靠近的]和},截取出來後依然是按逗號截取出最小部分”a”:”1″來判斷格式,如果對你有幫助的話記得點贊哦
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/231403.html