本文整理汇总了Python中qgis.core.QgsJSONUtils.encodeValue方法的典型用法代码示例。如果您正苦于以下问题:Python QgsJSONUtils.encodeValue方法的具体用法?Python QgsJSONUtils.encodeValue怎么用?Python QgsJSONUtils.encodeValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsJSONUtils
的用法示例。
在下文中一共展示了QgsJSONUtils.encodeValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testEncodeValue
# 需要导入模块: from qgis.core import QgsJSONUtils [as 别名]
# 或者: from qgis.core.QgsJSONUtils import encodeValue [as 别名]
def testEncodeValue(self):
""" test encoding various values for use in GeoJSON strings """
self.assertEqual(QgsJSONUtils.encodeValue(NULL), "null")
self.assertEqual(QgsJSONUtils.encodeValue(5), "5")
self.assertEqual(QgsJSONUtils.encodeValue(5.9), "5.9")
self.assertEqual(QgsJSONUtils.encodeValue(5999999999), "5999999999")
self.assertEqual(QgsJSONUtils.encodeValue("string"), '"string"')
self.assertEqual(QgsJSONUtils.encodeValue("str\ning"), '"str\\ning"')
self.assertEqual(QgsJSONUtils.encodeValue("str\ring"), '"str\\ring"')
self.assertEqual(QgsJSONUtils.encodeValue("str\bing"), '"str\\bing"')
self.assertEqual(QgsJSONUtils.encodeValue("str\ting"), '"str\\ting"')
self.assertEqual(QgsJSONUtils.encodeValue("str\\ing"), '"str\\\\ing"')
self.assertEqual(QgsJSONUtils.encodeValue("str\\ning"), '"str\\\\ning"')
self.assertEqual(QgsJSONUtils.encodeValue("str\n\\\\ing"), '"str\\n\\\\\\\\ing"')
self.assertEqual(QgsJSONUtils.encodeValue("str/ing"), '"str\\/ing"')
self.assertEqual(QgsJSONUtils.encodeValue([5, 6]), "[5,6]")
self.assertEqual(QgsJSONUtils.encodeValue(["a", "b", "c"]), '["a","b","c"]')
self.assertEqual(QgsJSONUtils.encodeValue(["a", 3, "c"]), '["a",3,"c"]')
self.assertEqual(QgsJSONUtils.encodeValue(["a", "c\nd"]), '["a","c\\nd"]')
self.assertEqual(QgsJSONUtils.encodeValue({"key": "value", "key2": 5}), '{"key":"value",\n"key2":5}')
self.assertEqual(
QgsJSONUtils.encodeValue({"key": [1, 2, 3], "key2": {"nested": "nested\\result"}}),
'{"key":[1,2,3],\n"key2":{"nested":"nested\\\\result"}}',
)
示例2: print
# 需要导入模块: from qgis.core import QgsJSONUtils [as 别名]
# 或者: from qgis.core.QgsJSONUtils import encodeValue [as 别名]
from qgis.utils import iface
geojson_contributors = os.path.join(
os.path.dirname(QgsApplication.developersMapFilePath()),
'contributors.json'
)
geojson_contributors_string = codecs.open(
geojson_contributors,
encoding='utf-8'
).read()
layer = iface.activeLayer()
# Encodes a value to a JSON string representation, adding appropriate
# quotations and escaping where required.
print(QgsJSONUtils.encodeValue([{"name": "George", "age": 34, "size": 1.69}]))
fields = QgsFields()
fields_list = [
QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double)
]
for f in fields_list:
fields.append(f)
feature = QgsFeature(fields)
feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(60, 5)))
feature.setAttributes(["George", 34, 1.69])