如何显示JSON列表是否可读?

时间:2017-12-10 15:02:22

标签: python json firebase

我在Firebase中有一个JSON字符串。我通过Pythons Pyrebase获取此信息 但是,这是一个很大的问题。

JSON I&gettting是一个列表,例如清单:

u'Wardrode u'No u'Behind Table u'No u'dust-dree

这是一个Json字符串,但我替换了一些正在使用的字符串:

 Using 

    result1 = result1.replace("u'", "")
    result1 = result1.replace("{", "")
    result1 = result1.replace("}", "")
    result1 = result1.replace("'", "")
    result1 = result1.replace(":", "")
    result1 = result1.replace("\u", "")

我想用换行符替换“是”和“否”,以便在我以HTML格式显示时成为正确的列表

这是我的完整代码:

    result1 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-in/Inspector/', None)

    for key in result1.keys():
        key = sorted(result1.keys())
        currentdate = key

        print currentdate

    for key in result1.keys():
       inspector = key


    print result1

我想使用我从firebase获取的单行json列表来创建一个体面且可读的输出。

看起来像这样:

{
  "Rooms" : {
    "Room1" : {
      "2017-11-29" : {
        "Inspection" : {
          "Scan-in" : {
            "Inspector" : {
              "brix" : {
                "Nov 29, 2017 11:20:15 PM" : {
                  "Checklist" : {
                    "1 Bible, Environment Card, Telephone Directory,  Amenity Card, Room Attendant’s Card,  Asmara Spa Tent Card, TV remote?" : "No",
                    "1 bath gel?" : "No",
                    "1 bath mat hanging on shower glass door handle?" : "No",
                    "1 bath soap" : "No",
                    "1 extra pillow with pillow cover?" : "No",
                    "1 facial tissue in a tissue box" : "No",
                    "1 lotion" : "No",
                    "1 pringles, 1 cashew nut, 1 cup noodles (placed in the coffee tray on the writing desk)?" : "No",
                    "1 sanitary bag" : "No",
                    "1 set of iron and board?" : "No",
                    "1 set of laundry list and bag?" : "No",
                    "1 shampoo?" : "No",
                    "1 shower cap" : "No",
                    "1 vanity kit" : "No",
                    "2 Tumbler overturned with coaster" : "No",
                    "2 bath towels - on the towel rack?" : "No",
                    "2 bathrobes?" : "No",
                    "2 coke, 2 sprite, 1 C2 lemon, 1 C2 apple, 1 pineapple juice, 1 orange juice, 1 mineral water, 2 San Mig light, 2 pale pilsen?" : "No",
                    "2 cups and saucers (for Mountain Wing" : "No",
                    "2 dental kit" : "No",
                    "2 face towels – on vanity counter (For suites, premier and VIPs)?" : "No",
                    "2 hand towels ?" : "No",
                    "2 mugs overturned with coaster (for Lake Wing)?" : "No",
                    "2 pairs of slippers?" : "No",
                    "6 Hangers?" : "No",
                    "Air conditioning temperature is in good working condition  Set at 17o C, low speed" : "No",
                    "Appearance door surface in good condition" : "Yes",
                    "Appearance door surface- in good condition" : "No",
                    "Appearance of the bed – neat and sheet tightened?" : "No",
                    "Arrangement of the sofa and table  in order" : "No",
                    "Bedhead free from dust" : "No",
                    "Bedsheet and pillow cases clean" : "No",
                    "Behind table - dust" : "No",
                    "Ceiling – no cobweb, water marks and crack lines" : "No",
                    "Check appearance of the glass – clean" : "No",
                    "Check the table surface – clean" : "No",
                    "Check underneath cushion" : "No",
                    "Coffee tray with 3 coffee sticks, 3 creamer, 3 white sugar, 3 brown sugar, 1 equal or sweetener, 3 lipton tea, 2 mineral water, 3 cocktail napkin ?" : "No",
                    "Compendium with 1 stationary pad, 1 envelope, channel guide?" : "No",
                    "Curtains are neat, presentable with hooks all intact and curtain rod" : "No",
                    "Door surface in good condition" : "No",
                    "Door surface – clean" : "No",
                    "Drawers – clean and dust-free" : "No",
                    "Dust bin with liner" : "No",
                    "Dustbin under the table is clean" : "No",
                    "Elsafe open or working?" : "No",
                    "Entrance louver clean and dust-free" : "No",
                    "Eye viewer and fire escape plan in order" : "No",
                    "Floor vacuumed and stain free" : "No",
                    "Free from rubbish under the bed" : "No",
                    "Fridge is cold and clean" : "No",
                    "Glass panel and door -clean" : "No",
                    "Guest Comment Card?" : "No",
                    "Hairdryer" : "No",
                    "Handbasin, vanity counter, vanity mirror and shaving mirror – clean" : "No",
                    "Happenings This Week" : "No",
                    "Key card holder – in working order" : "No",
                    "Lamp or light" : {
                      "diffuser clean and working" : "No"
                    },
                    "Let the door close by itself to test the door closure – in working order" : "No",
                    "Lights and switches are working" : {
                      "Remarks" : ""
                    },
                    "Lights in working order" : "No",
                    "Luggage bench fabric top is clean" : "No",
                    "Magazine" : "No",
                    "Minibar Voucher or Slip" : "No",
                    "Pencil?" : "No",
                    "Privacy Sign or Make Up Room Sign" : "No",
                    "Pull the curtains to test if they are working" : "No",
}
}

我希望通过删除不必要的文本使其可读和易懂。我可以通过

来做到这一点
    result1 =  str(result1)
    result1 = result1.replace("u'", "")
    result1 = result1.replace("{", "")
    result1 = result1.replace("}", "")
    result1 = result1.replace("'", "")
    result1 = result1.replace(":", "")
    result1 = result1.replace("\u", "")

这使我在第一张图片中输入的内容为:

enter image description here

但是我无法做到:

    result1 = result1.replace("No,", "No\n")
    result1 = result1.replace("Yes,", "Yes\n")

我想做的就是整理清单。 如果字符串找到“是”或“否”,则必须在换行符上。但仍显示其值。

我也欢迎任何有关如何对这些数据进行排序的新想法。

我想要的示例数据:

扫入:brix Nov 29, 2017 112015 PM Checklist Coffee tray with 3 coffee sticks, 3 creamer, 3 white sugar, 3 brown sugar, 1 equal or sweetener, 3 lipton tea, 2 mineral water, 3 cocktail napkin ? No

Luggage bench fabric top is clean No

1 facial tissue in a tissue box No

Towel Reminder No, 1 pringles, 1 cashew nut, 1 cup noodles (placed in the coffee tray on the writing desk)? No

......等等。

我该怎么做?请帮忙

1 个答案:

答案 0 :(得分:0)

  

这是我的完整代码

我不这么认为......从你给出的json和显示的代码,看起来你跳过了brix键。并且显示的图像不是控制台输出,因此您没有显示HTML生成。

无论如何,你的字符串替换只是错误的解决方案。您正在查看字典中Python的unicode字符串。如果您解析了内容,则不应看到u''{}个字符。

例如,尝试这样的事情

x = "'brix" 
print("Scan in " + x) 
dates = sorted(result1[x].keys())
for d in dates:
    print(d) 
    for check, status in result1[x][d]['Checklist'].items():
        print(check, status)