为什么 Json 对象在下面的 Json 值之前出现?

我正在尝试使用创建嵌套的 json 对象org.json.simple.JSONObject。为什么 jsonobject 改变顺序?


预期输出:


{  

  "id":"14",   

  "email":"xxx@gmail.com",    

  "Company":{   

    "address":"milton street",   

    "postal code":"cincinnati",   

    "name":"abc"  

  }  

}  

电流输出:


{  

"Company":{   

"address":"milton street",    

"postal code":"cincinnati",    

"name":"abc"  

},   

"id":"14",   

"email":"xxx@gmail.com"  

这是我的代码:


 JSONObject First = new JSONObject();

    First.put("id", "14");

    First.put("email", "xxx@gmail.com");


    JSONObject companydetails = new JSONObject();

    companydetails.put("name", "abc");

    companydetails.put("address", "milton street");

    companydetails.put("postal code", "cincinnati");


    First.put("Company",companydetails);


    System.out.println(First.toString());


至尊宝的传说
浏览 105回答 4
4回答

慕雪6442864

使用 google/gson 库JsonObject o=new JsonObject();o.addProperty("id", "14");o.addProperty("email", "xxx@gmail.com");JsonObject companydetails1 = new JsonObject();companydetails1.addProperty("name", "abc");companydetails1.addProperty("address", "milton street");companydetails1.addProperty("postal code", "cincinnati");o.add("Company",companydetails1);System.out.println(o.toString());使用 Maven 回购&nbsp; &nbsp; <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --><dependency>&nbsp; &nbsp; <groupId>com.google.code.gson</groupId>&nbsp; &nbsp; <artifactId>gson</artifactId>&nbsp; &nbsp; <version>2.3.1</version></dependency>或者用外部jar下载

慕桂英3389331

在此处查看答案:JSON 顺序混淆您不能也不应该依赖 JSON 对象中元素的顺序。来自http://www.json.org/的 JSON 规范:“对象是一组无序的名称/值对”因此,JSON 库可以根据需要自由地重新排列元素的顺序。这不是错误。

繁星淼淼

JsonObject 不维护顺序,这并不重要,因为您将通过键访问 json。

缥缈止盈

import com.fasterxml.jackson.annotation.JsonProperty;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import lombok.Builder;import lombok.Getter;import org.junit.Test;public class TestJUnit {&nbsp; &nbsp; @Test&nbsp; &nbsp; public void exec() {&nbsp; &nbsp; &nbsp; &nbsp; Gson gson = new GsonBuilder().disableHtmlEscaping().create();&nbsp; &nbsp; &nbsp; &nbsp; String toPrint = gson.toJson(ToJsonFormat.builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .id(14)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .email("xxx@gmail.com")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .company(Company.builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .address("milton street")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .postCode("cincinnati")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .name("abc")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(toPrint);&nbsp; &nbsp; }&nbsp; &nbsp; @Getter&nbsp; &nbsp; @Builder&nbsp; &nbsp; private static class ToJsonFormat {&nbsp; &nbsp; &nbsp; &nbsp; private int id;&nbsp; &nbsp; &nbsp; &nbsp; private String email;&nbsp; &nbsp; &nbsp; &nbsp; private Company company;&nbsp; &nbsp; }&nbsp; &nbsp; @Getter&nbsp; &nbsp; @Builder&nbsp; &nbsp; private static class Company {&nbsp; &nbsp; &nbsp; &nbsp; private String address;&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty("post code")&nbsp; &nbsp; &nbsp; &nbsp; private String postCode;&nbsp; &nbsp; &nbsp; &nbsp; private String name;&nbsp; &nbsp; }}结果:{"id":14,"email":"xxx@gmail.com","company":{"address":"milton street","post_code":"cincinnati","name":"abc"}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java