如何使用java在多维数组中推送一个对象?

时间:2018-01-04 12:31:37

标签: java arrays object arraylist multidimensional-array

我想在多维数组中推送一个对象。我知道 javascript ..我有javascript示例..但我不知道如何在 java

中使用该方法

Javascript示例:



var obj = [{
	"plicenceKey": "BI-NBPM39",
	"LicenceList": [{
		"billDurationName": "Month(s)",
		"planName": "jhkhjg",
		"licenceCreatedDate": "2018fdg01-02 12:14:56",
		"vehicleId": "90",
		"trailperday": "0",
		
	},{
		"billDurationName": "ffdhfg(s)",
		"planName": "jhkhjg",
		"licenceCreatedDate": "fdghdfg-01-12:14:56",
		"vehicleId": "78",
		"trailperday": "0",
		
	},{
		"billDurationName": "dfhfgh(s)",
		"planName": "jhkhjg",
		"licenceCreatedDate": "dfghfg-01-02 12::56",
		"vehicleId": "67",
		"trailperday": "0",
		
	}]
}]

var b = {
		"billDurationName": "test",
		"planName": "ghgfd",
		"licenceCreatedDate": "ghf-01-02 12::56",
		"vehicleId": "34",
		"trailperday": "0",
		
	};

obj[0].LicenceList.push(b)




如何用java写?请在java中给出一些例子..

5 个答案:

答案 0 :(得分:0)

您必须将地图用作ciconst 这是示例

obj

答案 1 :(得分:0)

1许可证将是一个名为License的单独对象,其中包含相应的字段。然后集合(在你的情况下称为ListLicense),将是java List(ArrayList)。然后你就可以做到:

myList.add(newLicense);

其中newLicense将是您的新许可证对象。

但是,如果它是由licenseKey映射的,那么你需要一个Map,而不是List

答案 2 :(得分:0)

但是要将对象推送到对象的特定索引..

例如



var obj = [  
   {  
      "plicenceKey":"key1",
      "LicenceList":[  
         {  
            "billDurationName":"Month(s)",
            "planName":"jhkhjg",
            "licenceCreatedDate":"2018fdg01-02 12:14:56",
            "vehicleId":"90",
            "trailperday":"0"
         },
         {  
            "billDurationName":"ffdhfg(s)",
            "planName":"jhkhjg",
            "licenceCreatedDate":"fdghdfg-01-12:14:56",
            "vehicleId":"78",
            "trailperday":"0"
         },
         {  
            "billDurationName":"dfhfgh(s)",
            "planName":"jhkhjg",
            "licenceCreatedDate":"dfghfg-01-02 12::56",
            "vehicleId":"67",
            "trailperday":"0"
         }
      ]
   },
   {  
      "plicenceKey":"key2",
      "LicenceList":[  
         {  
            "billDurationName":"Month(s)",
            "planName":"jhkhjg",
            "licenceCreatedDate":"2018fdg01-02 12:14:56",
            "vehicleId":"90",
            "trailperday":"0"
         }
      ]
   }
];

var b = {
		"billDurationName": "test",
		"planName": "ghgfd",
		"licenceCreatedDate": "ghf-01-02 12::56",
		"vehicleId": "34",
		"trailperday": "0",
		
	};

obj[1].LicenceList.push(b)




: 我必须将对象b 添加到包含 LicenceList plicenceKey:key2 的数组中。

答案 3 :(得分:0)

您可以使用Gson库

反序列化多维json对象
Gson gson = new GsonBuilder().create();
String[][] multiIntArray
 = new String[][]{{"1", "2", "3"}, {"4", "5", "6"}};

// serialization
gson.toJson(multiIntArray);   

// deserialization
String[][] intArray2
= gson.fromJson("[["1","2","3"],["4","5","6"]]", String[][].class);

答案 4 :(得分:0)

在Java中处理这种结构的最佳方法是使用各种第三方JSON库之一。

但是,如果您想要直接答案和JavaScript代码的直接Java代码......

obj初始化为List<Map<String,Object>>

它包含一个元素,它是一个带有两个条目的Map。第一个条目有一个String键,映射到String值。第二个条目有一个String键,映射到List Map个,每个地图都有字符串键和值(因此它是List<Map<String,String>>)。< / p>

这里是与您的代码等效的直接Java:

List<Map<String,Object>> obj =
    new ArrayList<>(List.of(
        new HashMap<>(Map.of(
            "plicenceKey", "BI-NBPM39",
            "LicenceList", new ArrayList<>(List.of(
                Map.of(
                    "billDurationName": "Month(s)",
                    "planName": "jhkhjg",
                    "licenceCreatedDate": "2018fdg01-02 12:14:56",
                    "vehicleId": "90",
                    "trailperday": "0"),
                Map.of(...),
                ...))))));

Map<String,String> b = Map.of(
    "billDurationName": "test",
    "planName": "ghgfd",
    "licenceCreatedDate": "ghf-01-02 12::56",
    "vehicleId": "34",
    "trailperday": "0");

obj.get(0).get("LicenceList").add(b);

顺便说一下,为方便起见,我使用了新的Java 9集合文字。如果您没有Java 9,则可以在程序上将每个元素添加到集合中。