从以以下格式存储的缓存中检索
{address={address1=string, address2=string, addressId=2045, city=string, fxGeocode=string,
houseNumber=string, isActive=true, postalCode=string, state=string, streetName=string, zip=string},
externalIds=[{externalId=string, externalIdDocId=38915437-69d7-449a-9bd8-9832c78b2010,
partyRoleExternalIdType=string}], name=string, organizationRoleId=990}
我必须将其转换回json对象。最好的方法是什么?
转换后的json对象采用以下格式
{
"address": {
"address1": "string",
"address2": "string",
"addressId": "2045",
"city": "string",
"fxGeocode": "string",
"houseNumber": "string",
"isActive": "true",
"postalCode": "string",
"state": "string",
"streetName": "string",
"zip": "string"
},
"externalIds": [
{
"externalId": "string",
"externalIdDocId": "38915437-69d7-449a-9bd8-9832c78b2010",
"partyRoleExternalIdType": "string"
}
],
"name": "string",
"organizationRoleId": "990"
}
答案 0 :(得分:1)
我会用Jackson JSON库来做。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.1</version>
</dependency>
这应该为您提供所需的输出:
ObjectMapper mapper = new ObjectMapper();
Map<String, Map<String, String>> myMap = new HashMap<>();
// add stuff to your map of maps
// pretty print
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(myMap);
System.out.println(json);
答案 1 :(得分:0)
您可以使用com.fasterxml.jackson.core库。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
and use objectMapper to convert this object into JSON.
//Object to JSON in String
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(object);
// beautify JSON
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
答案 2 :(得分:0)
您可以使用#include <string>
using namespace std;
class MyFloat {
protected:
char* mData;
public:
MyFloat(float v) {
cout << "using constructer\n";
mData = new char[10];
int k=snprintf(mData, sizeof(mData),"%f", v);
}
operator char *() const {
cout << "using convert\n";
return _strdup(mData);
}
~MyFloat() {
cout << "using des\n";
delete[] mData;
}
MyFloat(const MyFloat& a) {
cout << "using copy\n";
mData = new char[strlen(a.mData) + 1];
mData = _strdup(a.mData);
}
MyFloat& operator=(const MyFloat& a) {
cout << "using operator\n";
mData = new char[strlen(a.mData) + 1];
mData = _strdup(a.mData);
return *this;
}
friend istream& operator>>(istream& in, MyFloat& a) {
float t;
in >> t;
snprintf(a.mData, sizeof(a.mData), "%f", t);
return in;
}
friend ostream& operator<<(ostream& out, const MyFloat& a) {
out.write(a.mData, sizeof(a.mData));
return out;
}
static MyFloat ValueOf(float value) {
cout << "using static\n";
MyFloat temp(value);
return temp;
}
};
int main() {
char* sValue = MyFloat::ValueOf(123.45);
cout << sValue;
MyFloat temp(2.3);
cin >> temp;
cout << temp;
getchar();
return 0;
}
类将Java类对象转换为jason,将json转换为Java类对象
ObjectMapper
输出
{“ address”:{“ address1”:“ string”,“ address2”:“ string”}}
答案 3 :(得分:0)
Gson是Google提供的用于json序列化/反序列化的优秀库。 Maven依赖项:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
示例序列化:
Gson gson = new Gson();
Map<String, Map<String, String>> someMap = new HashMap<>();
try (FileWriter writer = new FileWriter("C:\\folder\\file.json")) {
gson.toJson(someMap, writer);
} catch (IOException e) {
e.printStackTrace();
}