将Dynamic json对象映射到java

时间:2016-08-25 20:38:21

标签: java json jackson

我正在构建一个REST API,其有效负载具有一个名为jsonContent的属性,该属性包含任何有效的json。

//# for: Define INPUT unit
var inputNum_Rate : Number = 0;     
var inputNum_Velo : Number = 0;

//# for: Define INPUT unit type (format)
var format_Rate : String = "ltr_pMin"; //set: "ltr_pMin" (Litres)or "gal_pMin" (Gallons)
var format_Diam : String = "mm"; //set: "mm" or "inches"
var format_Velo : String = "m_pSec"; //set: "m_pSec" (meters) or "ft_pSec" (feet)

var offset_Rate : Number = 16.66666666666666666; //Litres per Min
var offset_Velo : Number = 100.0; //Metres per Sec

var nvel : Number = 0;  var svel : Number = 0;
var nflow : Number = 0; var sflow : Number = 0;

var s : Number = 0;
var resultNum  : Number = 0; // holds the result

////////////// TEST APP /////////////////

//# 1) setup INPUT TYPE
format_Rate = "ltr_pMin"; //# Rate //set: "ltr_pMin" (Litres)or "gal_pMin" (Gallons)
format_Diam = "mm"; //# Diameter //set: "mm" or "inches"
format_Velo = "m_pSec"; //# Velocity //set: "m_pSec" (meters) or "ft_pSec" (feet)

//# 2) setup INPUT values ("Rate" & "Velocity" to get middle "Diameter");
inputNum_Rate = 100; inputNum_Velo = 4.5; //# These should be from input textbox

//# 3) calculate Diameter based on inputs of Rate & Velocity
resultNum = calculate_Diameter( inputNum_Rate, inputNum_Velo );
trace ("final :: resultNum : " + resultNum );

function calculate_Diameter( myRate:Number, myVelo:Number ) : Number
{
    //# RESET
    svel = nvel = nflow = sflow = s = resultNum = 0; //reset

    sflow = myRate * offset_Rate;
    svel = myVelo * offset_Velo;
    s = sflow/svel; //get Area
    s = Math.sqrt(4 * s / Math.PI);

    //# CONVERT
    s = s * 10;     s = Number(s.toPrecision(5));

    //# CHECK RESULTS IN DEBUGGER....
    trace("===========================================" );
    trace("\n FLOW RATE : " );
    //convert Litres and US gallons :: US-Gallons = ( litres / 3.79) || Litres = (3.79 * US-Gallons)
    trace("litres p/sec : " + ( myRate ) );
    trace("gallon p/sec : " + ( myRate / 3.79 ) );

    trace("\n BORE SIZE : " );
    //convert Mm and Inches :: (1 mm = 0.0393701 inch || 1 inch = 25.4 mm)
    trace("millms : " + (s) );
    trace("inches : " + (s * 0.0393701) );

    trace("\n VELOCITY : " );
    //convert Metres and Feet :: (1 metre= 3.28084 foot || 1 foot= 0.3048 metres)
    trace("m/sec  : " + ( myVelo ) );
    trace("ft/sec : " + ( myVelo * 3.28084 ) );
    trace("===========================================" );


    //# RETURN result of this function
    return resultNum;

}

在服务器端我想将它映射到通用java对象,最后将整个对象保存到mongodb

{
  "name":"foo",
  "jsonContent":{ "abc":"My content"}
}

我使用jackson将json映射到java。如何声明我的java对象,以便可以使用任何json内容。

2 个答案:

答案 0 :(得分:1)

使用JsonNode

private JsonNode jsonContent;

答案 1 :(得分:1)

我回答了我自己的问题,以下对我来说工作得很好

private Map<String,Object> jsonContent;