我有一个像这样的json数组:
数据:
[
{
image_url:"www",
testimonial_text: "The standard chunk of Lorem Ipsum used since the,
name: "Rohith",
designation: "Architect"
}
]
但我需要使用java转换它:
数据:
[
{
image_url:"www",
testimonial_text: "The standard chunk of Lorem Ipsum used since the,
name: "Rohith",
data2:[{ designation: "Architect"}]
}
]
答案 0 :(得分:1)
使用Jackson JSON API解析器https://github.com/FasterXML/jackson
while ($conn->more_results()){
$conn->next_result();
echo $conn->error;
}
<强> RESULT 强> [{“image_url”:“www”,“testimonial_text”:“Lorem Ipsum的标准块,因为”,“名称”:“Rohith”,“data2”:[{“names”:“Architect”}]} ,{“image_url”:“www”,“testimonial_text”:“Lorem Ipsum使用的标准块”,“name”:“Rohith”,“data2”:[{“names”:“Architect”}]}]
答案 1 :(得分:0)
JsonArray data = new JsonArray("<Data String>");
JsonObject data2 = new JsonObject ();
data2 .put("designation" , "Architect");
JSONArray arr = new JSONArray();
arr .put(data2 );
data.getJSONObject(0).put("data2" , arr );
答案 2 :(得分:0)
我强烈建议您使用最新的标准JavaEE8 API JSON-P 1.1(JSR374)
Glassfish已经有了实施。
Maven依赖:
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
或者您可以直接下载以下两个罐子并导入到您的项目中:
这是可运行的代码快照:
String data = "[{\"image_url\":\"www\","
+ "\"testimonial_text\": \"The standard chunk of Lorem Ipsum used since the\","
+ " \"name\": \"Rohith\","
+ "\"designation\": \"Architect\"}]";
JsonArray original = Json.createReader(new StringReader(data)).readArray();
JsonArray changes = Json.createArrayBuilder().add(Json.createObjectBuilder().add("designation", "Architect"))
.build();
JsonPatchBuilder builder = Json.createPatchBuilder();
JsonArray target = builder.remove("/0/designation").add("/0/data2", changes).build().apply(original);
System.out.println(target.toString());
注意:You need add double quotation marks at the key field
这是输出:
[{&#34; image_url&#34;:&#34; www&#34;,&#34; testimonial_text&#34;:&#34; Lorem的标准块 从那以后使用了Ipsum 所述&#34;&#34;名称&#34;:&#34; Rohith&#34;&#34; DATA2&#34;:[{&#34;指定&#34;:&#34;建筑师&#34 ;}]}]