我正在使用Google Natural Language API为带有情绪分析的项目标记文本.我想将我的NL结果存储为JSON.如果向Google发出直接HTTP请求,则会返回JSON响应.
但是,当使用提供的Python库时,将返回一个对象,并且该对象不是直接JSON可序列化的.
以下是我的代码示例:
import os
import sys
import oauth2client.client
from google.cloud.gapic.language.v1beta2 import enums, language_service_client
from google.cloud.proto.language.v1beta2 import language_service_pb2
class LanguageReader:
# class that parses, stores and reports language data from text
def __init__(self, content=None):
try:
# attempts to autheticate credentials from env variable
oauth2client.client.GoogleCredentials.get_application_default()
except oauth2client.client.ApplicationDefaultCredentialsError:
print("=== ERROR: Google credentials could not be authenticated! ===")
print("Current enviroment variable for this process is: {}".format(os.environ['GOOGLE_APPLICATION_CREDENTIALS']))
print("Run:")
print(" $ export GOOGLE_APPLICATION_CREDENTIALS=/YOUR_PATH_HERE/YOUR_JSON_KEY_HERE.json")
print("to set the authentication credentials manually")
sys.exit()
self.language_client = …Run Code Online (Sandbox Code Playgroud) python serialization json google-cloud-platform google-cloud-nl