【问题标题】:Translating json to string variable将 json 转换为字符串变量
【发布时间】:2020-11-26 13:58:14
【问题描述】:

我正在开发一个 web 服务,可以让两个应用程序相互通信,第一个应用程序将向第二个应用程序发送一个 json 对象。 我一直在翻译这个 json 对象:

$body = "{"fields":{"project":{"key":"'+$projectKey+'"}
,"issuetype":{"name": "'+$issueType+'"}
,"summary":"'+$summary+'"
,"description":"'+$description+'"
,"customfield_12721":"'+$FirstName+'"
,"customfield_12722":"'+$LastName+'"
,"customfield_12723":{"value":"'+$EmployeeCategory+'"}
,"customfield_12732":"'+$Externalfunction+'"
,"customfield_12725":"'+$CorporateID+'"
,"customfield_12726":{"value":"'+$VermegCompany+'"}
,"customfield_12685":{"value":"'+$IndusRegion+'"}
,"customfield_12673":{"value":"'+$Product+'"}
,"customfield_12727":{"value":"'+$Profile+'"}
,"customfield_12667":{"name":"'+$Manager+'"}
,"customfield_12708":"'+$BeginDate+'"
,"customfield_14000":"'+$Reglementation+'"
,"customfield_14001":"'+$Department+'"
,"customfield_14002":"'+$SubDepartment+'"} 

}";

到像这样的字符串变量:

String json = "{fields:{project:{\"key\":\""+ projectkey +"}"       
         + "\",\"issuetype\":\"" 
         + "\",\"customfield_12721\":\"" + employee.getFirstName()
         + "\",\"description\":\"" + description
         + "\",\"summary\":\"" + summary    
         + "\",\"customfield_12722\":\""+ employee.getLastName()
         + "\",\"customfield_12732\":\"" + employee.getFte()
         + "\",\"customfield_14000\":\"" + employee.getReglementation()
         + "\",\"customfield_14001\":\"" + employee.getDepartment()
         + "\",\"customfield_14002\":\"" + employee.getSubdepartment()
         + "\",\"fulltime\":" + Math.round(Double.parseDouble(employee.getFulltime().replaceAll(",",".")))
         //+ ",\"email\":\"" + employee.getEmail()
         +   ",\"citizenship\":\"" + employee.getCitizenship()
         + "\",\"gnn\":\""+ employee.getGnn()
         + "\",\"company\":\"" + employee.getCompany()
         + "\",\"employeeid\":\"" + employee.getEmployeeid()
         + "\",\"customfield_12708\":\"" + employee.getStartdate()
         //+ "\",\"enddate\":\"" //+ employee.getEnddate()
    
         + "\",\"product\":\"" + employee.getProduct()
         + "\",\"customfield_12725\":\"" + employee.getInternalnumber()
        // + "\",\"employeeid\":\"" + employee.getEmployeeid()
         + "\"}}";

你能帮忙吗?

【问题讨论】:

  • 两者都是Java? (看起来像 JIRA)。试过杰克逊 JSON 映射器?你试过什么?您应该提供更多代码。你想要什么并不是很明显。

标签: java json web-services


【解决方案1】:

这里你可以使用JsonConvert库来获取json数据。下面是如何使用它的例子

String json = "{\"FirstName\":\"Jack\",\"LastName\":\"Tor\"}";

            var data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

            Console.WriteLine(data);
            Console.ReadLine();

【讨论】:

    【解决方案2】:

    使用网络工具

    如果你只是想快速转换成字符串,你可以在网上使用Json to String converter。


    使用库

    但是,如果您想在应用程序中找到一个好的解决方案,解决此问题的一种方法是使用诸如 GSON 之类的库进行序列化和解析。当然,如果您不打算自己创建此转换。

    GSON 非常易于使用,可以为您完成翻译工作。请参阅下面的示例:

    发送应用程序:

    Gson gson = new Gson();
    String jsonStr = gson.toJson(employee); // Serialize (from Java class to JSON string)
    
    // Send data (jsonStr) ...
    

    接收应用程序:

    // Receive data (jsonStr) ...
    
    Gson gson = new Gson();
    Employee employee = gson.fromJson(jsonStr); // Parse (from JSON string to Java class)
    

    默认情况下,GSON 将字段命名为与 Java 类中的成员变量名称相同。如果需要更改JSON字符串中的字段名称,可以在成员变量前使用@SerializedName("newName")。

    例子:

    class Employee {
      ...
      @SerializedName("customfield_12721") String firstName;
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 2018-02-08
      • 2010-09-30
      • 2011-08-27
      相关资源
      最近更新 更多