0
{
 "type": "telemetry",
 "timestamp": 1234,
 "device": 5678,
 "value": { 
   [
     {"battery_level": 87},
     {"var2": 2},
     {"var3": 3} 
   ] 
  }
}

I want to pasre this json file in java code using jsonobject and jsonarray Here, I'm stuck up at values keyword inside has no key to array.

Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
punith
  • 11

1 Answers1

1

Your valid JSON would be -

{
 "type": "telemetry",
 "timestamp": 1234,
 "device": 5678,
 "value": 
   [
     {"battery_level": 87},
     {"var2": 2},
     {"var3": 3} 
   ] 

}

You can then use following JAVA POJO objects to parse your json into the java object -

import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"type",
"timestamp",
"device",
"value"
})
public class Example {

@JsonProperty("type")
private String type;
@JsonProperty("timestamp")
private Integer timestamp;
@JsonProperty("device")
private Integer device;
@JsonProperty("value")
private List<Value> value = null;

@JsonProperty("type")
public String getType() {
return type;
}

@JsonProperty("type")
public void setType(String type) {
this.type = type;
}

@JsonProperty("timestamp")
public Integer getTimestamp() {
return timestamp;
}

@JsonProperty("timestamp")
public void setTimestamp(Integer timestamp) {
this.timestamp = timestamp;
}

@JsonProperty("device")
public Integer getDevice() {
return device;
}

@JsonProperty("device")
public void setDevice(Integer device) {
this.device = device;
}

@JsonProperty("value")
public List<Value> getValue() {
return value;
}

@JsonProperty("value")
public void setValue(List<Value> value) {
this.value = value;
}

}
-----------------------------------com.example.Value.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"battery_level",
"var2",
"var3"
})
public class Value {

@JsonProperty("battery_level")
private Integer batteryLevel;
@JsonProperty("var2")
private Integer var2;
@JsonProperty("var3")
private Integer var3;

@JsonProperty("battery_level")
public Integer getBatteryLevel() {
return batteryLevel;
}

@JsonProperty("battery_level")
public void setBatteryLevel(Integer batteryLevel) {
this.batteryLevel = batteryLevel;
}

@JsonProperty("var2")
public Integer getVar2() {
return var2;
}

@JsonProperty("var2")
public void setVar2(Integer var2) {
this.var2 = var2;
}

@JsonProperty("var3")
public Integer getVar3() {
return var3;
}

@JsonProperty("var3")
public void setVar3(Integer var3) {
this.var3 = var3;
}

}

After your Java objects and JSON is in place use jackson to do the mapping -

ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{'name' : 'mkyong'}";

//JSON from file to Object
User user = mapper.readValue(new File("c:\\user.json"), Example.class);
Dev Utkarsh
  • 1,377
  • 2
  • 18
  • 43