如何使用JavaAzure函数解析JSON并插入Azure SQL
我是JavaAzure函数的新手。 我让服务总线触发器和Azure SQL输出正常工作,但现在需要解析json消息的建议 我有服务总线触发器,它提供类似{name:John,city:NYC}的数据 我想将此存储到Azure SQL。如何解析JSON并存储到SQL如何使用JavaAzure函数解析JSON并插入Azure SQL,java,azure-functions,Java,Azure Functions,我是JavaAzure函数的新手。 我让服务总线触发器和Azure SQL输出正常工作,但现在需要解析json消息的建议 我有服务总线触发器,它提供类似{name:John,city:NYC}的数据 我想将此存储到Azure SQL。如何解析JSON并存储到SQL package com.function; import com.microsoft.azure.functions.annotation.*; import com.microsoft.azure.functions.*; imp
package com.function;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
//import java.sql.ResultSet;
/**
* Azure Functions with Azure Storage Queue trigger.
*/
public class TopicTriggerSQLOutput {
/**
* This function will be invoked when a new message is received at the specified path. The
message contents are provided as input to this function.
*/
@FunctionName("TopicTriggerSQLOutput")
public void run(
@ServiceBusTopicTrigger(
name = "message",
topicName = "newtopic",
subscriptionName = "newsubscription",
connection = "topicconnstring"
) String message,
final ExecutionContext context
) {
String connectionUrl = "jdbc:sqlserver:...database.windows.net;loginTimeout=30;";
try (Connection connection = DriverManager.getConnection(connectionUrl);
Statement statement = connection.createStatement();) {
context.getLogger().info("SeviceBus message" + message); // I get JSON returned
correctly here.
// How to parse message json string?
// Create and execute a SELECT SQL statement.
String InsertSql = "INSERT INTO [dbo].[Target_Table] ([NAME],[CITY]) VALUES
('NameString', 'CityString')";
// insert the data
statement.executeUpdate(InsertSql);
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
context.getLogger().info("Message: " + message);
}
}
导入Gson解决了问题
你能告诉我你的错误吗?如果你找到了解决办法,请标记你自己的答案来结束这个问题。这将有助于其他人
import com.google.gson.Gson;
Student myjson = gson.fromJson(jsonString, myjson.class);