欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

json格式

程序员文章站 2024-01-13 11:43:10
...

0 字符串要""包裹
1 单个对象要在{} 中
2 多个对象要在[ {1}, {2}, {3}, {4}] 中
json格式


    def toJson(self):
        """
        将图保存为json格式
        :return:
        """
        
        id = " \"id\" : " + "\"" + self.id + "\""
        loc = "\"loc\" : " + "\"" + self.long + "_" + self.lat + "\""
        adjacent = "\"adjacent\" : ["
        for v, v_weight in self.adjacent.items():
            adjacent += "{ " + "\"adj_id\" : " + "\"" + str(v.id) + "\"" + "," \
                        + "\"loc\" : " + "\"" + v.long + "_" + v.lat + "\"" + "," \
                        + "\"weight\" : " + str(v_weight) + "} ,"
        adjacent = adjacent.rstrip(",") + ' ]'

        return "{" + id + ", " + loc + ", " + adjacent + " }"