如何在引号前不带反斜杠的情况下在python graphene解析器中返回json

约翰·奥弗龙

我在python(Flask + Graphene)中有一个后端服务器,我需要返回这样的JSON对象:

{
      's1': "Section 1",
      's2': "Section 2",
      's3': "Section 3",
      's4': "Section 4"
}

解析器如下所示:

 questionnaire = graphene.types.json.JSONString(
        description='JSON result test')

    def resolve_questionnaire(self, info: graphql.ResolveInfo):
        sections = {
          's1': "Section 1",
          's2': "Section 2",
          's3': "Section 3",
          's4': "Section 4"
        }

        print(json.dumps(sections))
        return sections

并在控制台中看到了print(json.dumps(sections))预期的结果

user-api_1  | {"s1": "Section 1", "s2": "Section 2", "s3": "Section 3", "s4": "Section 4"}

但是在GraphiQL中,我看到所有带反斜杠的引号: 在此处输入图片说明

当我将更改为时,return sectionsreturn json.dumps(sections)得到如下结果:在此处输入图片说明

问题是如何在石墨烯解析器中正确返回JSON对象?我知道有json.replace方法使用像这里,但我相信,我只是在生产/传递对象错误的方式。

托比E

您的初步结果

{
  "data": {
    "questionnaire": "{\"s1\": \"Section 1\", \"s2\": \"Section 2\", \"s3\": \"Section 3\", \"s4\": \"Section 4\"}"
  }
}

is the intended behavior. After all, questionnaire resolves to a JSONString. Since it is a string it must be double quoted, thus its inner quotations must be escaped. This is according to JSON's standards.

To use that string you, would have to run some sort of JSON parser on the data.questionnaire object. In javascript, for instance, it would be something like:

var data;
// Fetching logic to get the data object from your GraphQL server
var sections = JSON.parse(data.questionaire);

// Now you can access its objects
console.log(sections.s1) // Should print "Section 1" on the dev console

但是,如果sections没有预先确定密钥sections.s5可能在一种情况下定义,而在另一种情况下未定义,则上述方法并不理想相反,您可能希望有一个可以迭代的数组。为此,您必须定义一个具有显式键值对的“模型”。这样做也适用于GraphQL。例如:

import graphene

# Our new model
class Section(graphene.ObjectType):
    key = graphene.String()        # dictionary key
    header = graphene.String()     # dictionary value

# Your previous schema with modifications
class Query(graphene.ObjectType):
    # questionnaire = graphene.types.json.JSONString(description='JSON result test')

    # Return a list of section objects
    questionnaire = graphene.List(Section)

    def resolve_questionnaire(self, info: graphql.ResolveInfo):
        sections = {
          's1': "Section 1",
          's2': "Section 2",
          's3': "Section 3",
          's4': "Section 4"
        }

        sections_as_obj_list = [] # Used to return a list of Section types

        # Create a new Section object for each item and append it to list
        for key, value in sections.items(): # Use sections.iteritems() in Python2
            section = Section(key, value) # Creates a section object where key=key and header=value
            sections_as_obj_list.append(section)

        # return sections
        return sections_as_obj_list

现在,如果我们运行查询:

query {
  questionnaire {
    key
    header
  }
}

它返回一个可以迭代的JSON数组。

{
  "data" {
    "questionnaire": [
      {
        "key": "s1",
        "header": "Section 1"
      },
      {
        "key": "s2",
        "header": "Section 2"
      },
      {
        "key": "s3",
        "header": "Section 3"
      },
      {
        "key": "s4",
        "header": "Section 4"
      },
    ]
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在输出中不带反斜杠的情况下回显感叹号以防止扩展

如何在Ruby中处理JSON解析器错误

如何在没有重复值的情况下在python中获得前100个素数?

Python Json解析器

没有第三方解析器的情况下,如何在Kotlin中解析JSON?

如何使用GraphQL解析器中包含的JSON API?

如何在不使用Javascript注释的情况下在字符串中添加双斜杠?

如何在不带“标志”包的情况下在Go中获取命令行参数?

如何在不带微秒的情况下在Django中添加DateTimeField

如何在Java中的JSON解析器的字符串中转义双引号

如何在bash脚本中不带引号的情况下呈现变量?

groovy/json - 如何在巨大的 JSON 中仅返回所需对象的属性(有点像 stax 解析器??)

如何在graphql解析器中同时返回错误和数据?

如何在路由解析器Angular中返回内部可观察的结果

如何在 Apollo 解析器 (Meteor/Apollo/Graphql) 中返回聚合查询?

Angular 4如何在解析器中返回多个可观察对象

如何添加JSON解析器库?

如何使用android的Json解析器?

Scala解析器组合器:与“ into”组合时,如何返回中间解析器的内容?

如何在没有反走样的情况下在Photoshop中缩放位图?

如何在不使用json库和json解析器的情况下解析json文件

如何将解析器返回的数据合并到CombineLatest调用中?

python中的解析器XML

Python中的JavaScript解析器

Python中的解析器日期

Python中的文本解析器

如何在URL路径中不带斜杠的情况下实现angular2嵌套组件路由

如何在不使用“ -cd”启动选项的情况下在某个目录中启动rxvt实例(可能是“引号”错误)?

如何在c / c ++ / java中在源代码中不带双引号和撇号的情况下打印文本