python 포맷 Json 데이터
                                            
 4583 단어  Python
                    
왜 모듈을 직접 써서 호출해야 합니까? 문제가 발생했기 때문에 자체 json 모듈로 처리된 json 데이터 정렬이 수정되었습니다. json 포맷은 무질서하고 정렬은 호출에 중요하지 않지만 때때로 포맷된 json은 수요를 읽을 때 매우 싫어합니다. 아주 싫어요!!때때로 포맷 후 말미의 데이터가 시작하여 읽기 장애를 일으키기 때문에 본고는 스스로 모듈을 써서 json 데이터를 처리하여 기본 순서에 따라 데이터를 보여줄 수 있도록 한다.
구체적인 코드는 다음과 같습니다.
#!/usr/bin/python
# -*- coding: utf-8 -*-
# autor chenlong
import UserString
def formatJson(jsonStr):
    if (None == jsonStr and '' == jsonStr):
        return ''
    __bu =  UserString.MutableString('')
    __last = '\0'
    __current = '\0'
    __indent = 0
    isInQuotationMarks = False
    for i in jsonStr:
        __last = __current
        __current = i
        if(__current == '"'):
            if(__last != '\\'):
                isInQuotationMarks = not isInQuotationMarks
            __bu.append(__current)
        elif(__current == '{'):
            __bu.append(__current)
            if(not isInQuotationMarks):
                __bu.append('
')
                __indent += 1
                __addIndentBlank(__bu,__indent)
        elif(__current == '['):
            __bu.append(__current)
            if(not isInQuotationMarks):
                __bu.append('
')
                __indent += 1
                __addIndentBlank(__bu,__indent)
        elif(__current == '}'):
            if(not isInQuotationMarks):
                __bu.append('
')
                __indent -= 1
                __addIndentBlank(__bu,__indent)
            __bu.append(__current)
        elif(__current == ']'):
            if(not isInQuotationMarks):
                __bu.append('
')
                __indent -= 1
                __addIndentBlank(__bu,__indent)
            __bu.append(__current)
        elif(__current == ','):
            __bu.append(__current)
            if(__last != '\\' and (not isInQuotationMarks)):
                __bu.append('
')
                __addIndentBlank(__bu,__indent)
        else:
            __bu.append(__current)
    return __bu
def __addIndentBlank(bu,indent):
    for i in range(indent):
        bu.append('\t')end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.