开发者社区> 问答> 正文

码栈解析JSON字符串的方法

有些时候我们获取到的可能是一个json串,这时就可以用下面的方式来解析其中的数据了,而不用再使用正则表达式去匹配结果的。下面演示下如何获取指定键的值,以及数组是如何遍历访问的
func main()
        $jstr = '{"students":[{"name":"zhangsan"},{"name":"lisi"},{"name":"wanger"}]}'
        ;获取json中指定键的值
        $students = getJsonString($jstr, "students")
        J_MsgBox("students的值为 :" & $students)

        ;获取json数组的元素个数,或上标
        $count = getJsonArrayCount($students)
        J_Msgbox("students下共有:" & $count & "个json对象")

        ;循环读取json数组中每一个元素的数据
        for $i = 1 to $count
                ;获取json数组中指定位置的元素
                $curStudent = getJsonArrayString($students, $i)
                J_MsgBox("students数组中第" & $i & "个元素为:" & $curStudent)

                $name = getJsonString($curStudent, "name")
                J_MsgBox("第" & $i & "个学生的姓名为:" & $name)
        next
EndFunc   ;==>main






先在码栈中添加以下代码,有兴趣的可以将改进或更好的方法贴出来分享的哦 ;获取JSON串中指定键的值
;$jstr 要被解析的json字符串
;$key 要获取的json中的值的键名
func getJsonString($jstr, $key)
        Dim $start;用来记录键的位置
        ;尝试两种引号
        if indexOf($jstr, '"' & $key & '":') > 0 Then
                $start = indexOf($jstr, '"' & $key & '":')
        else
                $start = indexOf($jstr, "'" & $key & "':")
        EndIf
        ;如果没有找到键就直接返回0
        if $start = 0 then
                return 0
        Else
                $jstr = (subString($jstr, $start + 3 + lengthString($key), lengthString($jstr)));截取键后面的字符
                return getQuotedString($jstr)
        EndIf
EndFunc   ;==>getJsonString

Func getJsonArrayString($jstr, $index)
        Dim $tempString;当缓存使用
        ;简单较验,非json数组直接返回0
        if not(leftString($jstr, 1) = "[" and rightString($jstr, 1) = "]") Then
                return 0
        EndIf
        $tempString = subString($jstr, 2, lengthString($jstr) - 1);去掉两边的中括号
        Dim $count = 0;用来计数的
        Dim $job;用来存储结果
        For $i = 1 to $index
                $job = getQuotedString($tempString)
                If not $job = 0 Then
                        $tempString = subString($tempString, lengthString($job), lengthString($tempString))
                        if indexOf($tempString, ",") > 0 and(indexOf($tempString, "{") > indexOf($tempString, ",") or indexOf($tempString, "[") > indexOf($tempString, ",")) Then
                                $tempString = subString($tempString, indexOf($tempString, ",") + 1, lengthString($tempString))
                        EndIf
                EndIf
        Next
        return $job
EndFunc   ;==>getJsonArrayString

;获取json数组中的指定的元素
Func getJsonArrayCount($jstr)
        Dim $tempString;当缓存使用
        ;简单较验,非json数组直接返回0
        if not(leftString($jstr, 1) = "[" and rightString($jstr, 1) = "]") Then
                return 0
        EndIf
        $tempString = subString($jstr, 2, lengthString($jstr) - 1)
        Dim $count = 0;用来计数的
        while True
                $job = getQuotedString($tempString)
                If not $job = 0 Then
                        $count = $count + 1
                        $tempString = subString($tempString, lengthString($job), lengthString($tempString))
                        if indexOf($tempString, ",") > 0 Then
                                $tempString = subString($tempString, indexOf($tempString, ",") + 1, lengthString($tempString))
                        EndIf
                Else
                        Return $count
                EndIf

        WEnd
EndFunc   ;==>getJsonArrayCount

;得到用,或}终结的字符串
Func getQuotedString($jstr)
    Dim $paramStore;用来存储括号的
    Dim $result;用来存储结果
    $length = lengthString($jstr);先拿到长度
    $quote = '"'
    if indexOf($jstr, "'") > 0 then
        $quote = "'"
    EndIf
    if $length > 0 Then
        for $i = 1 to $length + 1;挨个字符判断
            Local $curString = subString($jstr, $i, $i);取出当前字符
            if(($curString = "," or $curString = "}") and lengthString($paramStore) = 0) or $i = $length + 1 Then;判断是否为结尾
                if leftString($result, 1) = "'" or leftString($result, 1) = '"' Then
                    $result = subString($result, 2, lengthString($result))
                EndIf
                if rightString($result, 1) = "'" or rightString($result, 1) = '"' Then
                    $result = subString($result, 1, lengthString($result) - 1)
                EndIf
                return $result
            elseif $curString == "{" or $curString == "[" or($curString == $quote and not(rightString($paramStore, 1) == $quote)) Then
                $paramStore = $paramStore & $curString
            elseif $curString == "}" and rightString($paramStore, 1) == "{" Then
                $paramStore = subString($paramStore, 1, lengthString($paramStore) - 1)
            elseif $curString == "]" and rightString($paramStore, 1) == "[" or($curString == $quote and(rightString($paramStore, 1) == $quote)) Then
                $paramStore = subString($paramStore, 1, lengthString($paramStore) - 1)
            EndIf
            $result = $result & $curString
        Next
    Else
        ;J_LOG("length of jstr is 0, will return 0")
        return 0
    EndIf
EndFunc   ;==>getQuotedString

;取子串,$start开始位置 $end结束位置
func subString($str, $start, $end)
        return J_String($str, "取子串", $start & "," & ($end - $start + 1))
EndFunc   ;==>subString

;取左子串
func leftString($str, $count)
        return J_String($str, "取左子串", $count)
EndFunc   ;==>leftString

;取右子串
func rightString($str, $count)
        return J_String($str, "取右子串", $count)
EndFunc   ;==>rightString

;字符长度
Func lengthString($str)
        return J_String($str, "长度", "")
EndFunc   ;==>lengthString

;查找指定字符所在位置
Func indexOf($str, $findStr)
        return J_String($str, "是否包含", $findStr)
EndFunc   ;==>indexOf






展开
收起
水晶心泉 2017-02-21 00:05:14 4353 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像