java中数组转换为json格式

java中数组转换为json格式
PricingRangeDTO[] shuzu={new PricingRangeDTO("asd"),new PricingRangeDTO("asd")};
StringBuffer sb=new StringBuffer();
List list=new ArrayList();
for(int i=0;i<shuzu.length;i++){
list.add(shuzu[i]);
}
String abc=JSONArray.fromObject(list).toString();
System.out.println(abc);
---------
很简单的方法,但是开始自己开发的时候,就只想找JSON里面的方法直接转化对象数组
不会换个方向思考!切记!

---------以下引用   oceanloong   的 json转对象数组与对象数组转json
String obj = "[{MName:\"aaa\",MValue:\"bbb\"}]";      
System.out.println(obj);
JSONArray array=JSONArray.fromObject(obj);
JsonConfig jsonconfig=new JsonConfig();
jsonconfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY );
jsonconfig.setRootClass(PropertyT.class);
PropertyT[] mProperties=(PropertyT[])JSONSerializer.toJava(array,jsonconfig);


for(int i=0;i<mProperties.length;i++){
System.out.println(mProperties[i].getMName());
System.out.println(mProperties[i].getMValue());
}




=======================
var objects;
function loadSearch()
{
var searchId=document.getElementById("searchId").value;
var ur="search.ao?method=postEditSearch&searchId="+searchId;
$.post(ur,null,loadSearchListener,"json");

}
function loadSearchListener(obj)
{
$("#txt_caption").attr("value",obj['MCaption']);
objects=obj;
$.post("search.ao?method=postInsertSearch",null,loadListener,"json");
}
function loadListener(obj)
{
var objs;
for(var n=0;n<obj.length;n++)
{
objs=objs+"<option value='"+obj[n]['MDocIndexFieldID']+"'>"+obj[n]['MFieldName']+"</option>";
}
$.each(objects.MSortFields,function(i){
$("#tab_sort").append ( '<tr id="tr_sort"><td id="td1_sort"><select id="'+objects.MSortFields[i].MDocIndexFieldID+'">'+objs+'</select></td><td id="td2_sort"><select name="sel_sortdirection" id="'+objects.MSortFields[i]['MSortOrder']+'"><option value="0">asc</option><option value="1">desc</option></select></td><td><img name="newSubmit'+s+'" id="newbuttonid'+s+'" onclick="del_tab_sort(this)" src="../images/delete.gif" style="cursor:hand"></img></td></tr>');
})
$.each(objects.MResults,function(i){
$("#tab_result").append( '<tr id="tr_result"><td id="td1_result"><input name="result1" id="result1id" type="text" value="'+objects.MResults[i].MCaption+'"></td><td id="td2_result"><input type="text" name="result2" id="result2id" value="'+objects.MResults[i].MDocIndexFieldID+'"></td><td id="td3_result"><select id="'+objects.MResults[i].MDocIndexFieldID+'">'+objs+'</select></td><td><img onclick="del_tab_result(this)" src="../images/delete.gif" style="cursor:hand"></img></td></tr>');

})
$.each(objects.MProperties,function(i){
$("#tab_property").append( '<tr id="tr_pro"><td id="td1_pro"><input name="property1" id="property1id" type="text" value="'+objects.MProperties[i]["MName"]+'"></td><td id="td2_pro"><input type="text" name="property2" id="property2id" value="'+objects.MProperties[i]["MValue"]+'"></td><td><img name="newSubmit" id="newbuttonid" onclick="del_tab_property(this)" src="../images/delete.gif" style="cursor:hand"></img></td></tr>');

})

var o=objs+"<option value='Specify field'>Specify field...</option>";
$.each(objects.MParameters,function(i){

$("#tab_parameter").append( '<tr><td>'+objects.MResults[i]["MCaption"]+'</td><td><input type="text" name="result2" id="result2id'+a+'" onclick="show_div_par(this)" readOnly></td><td id="divid"><div id="div_par" style="position:absolute;width:324px;height:275px;z-index:50;left: 133px;top: 37px;background-color:#FFFFFF;display:none"><div style="width:100%;background:blue;height:18px;color:blue;" onmousemove="divMove(this)"  ></div><table width="324" height="283" border="1" bgcolor="#C3DAF9" id="tab_div"><tr id="tr_caption"><td width="118">Caption:</td><td width="193" id="td_caption"><input type="text" id="caption" name="textfield" value="'+objects.MResults[i]["MCaption"]+'" /></td></tr><tr id="tr_indexField"><td>Index Field:</td><td id="td_indexField"><select  name="select" onchange="selchange(this)" id="'+objects.MResults[i]["MDocIndexFieldID"]+'">'+o+'</select><input type="text" id="seltxt" style="display:none" onBlur="insOption(this)"/></td></tr><tr id="tr_defaultValue"><td>Default Value:</td><td id="td_defaultValue"><input type="text" name="textfield2" value="'+objects.MResults[i]["MDefaultValue"]+'"/></td></tr><tr id="tr_type"><td>Input Type:</td><td id="td_type"><select name="select2" id="selType" onchange="showSize(this)"><option value="Text Field">Text Field</option><option value="Select Box">Select Box</option><option value="Date">Date</option><option value="Hidden">Hidden</option></select></td></tr><tr id="tr_typesize"><td>Input Size:</td><td id="td_typeSize"><input type="text" name="textfield3" id="txtSize" value=""/></td></tr><tr id="tr_comparison"><td>Comparison Operation:</td><td id="td_comparison"><select name="'+objects.MResults[i]["MOpType"]+'"><option value="Equal">Equal</option><option value="Less than">Less than</option><option value="Less than or equal">Less than or equal</option><option value="Greater than">Greater than</option><option value="Greater than or equal">Greater than or equal</option><option value="Like">Like</option></select></td></tr><tr id="tr_allowRanged"><td>Allow Ranged:</td><td id="td_allowRanged"><input type="checkbox" name="checkbox" value="'+objects.MParameters[i]["MAllowRanged"]+'" /></td></tr><tr id="tr_readOnly"><td>Read Only:</td><td id="td_readOnly"><input type="checkbox" name="checkbox2" value="'+objects.MParameters[i]["MReadOnly"]+'" /></td></tr><tr id="tr_required"><td>Required:</td><td id="td_required"><input type="checkbox" name="checkbox3" value="'+objects.MParameters[i]["MRequired"]+'" /></td></tr><tr><td><div align="center"><button type="button" name="Submit4">left</button><button type="button" name="Submit5">up</button><button type="button" name="Submit6">right</button></div></td><td><button type="button" name="Submit" >Save</button><button type="button" name="Submit2" onclick="hidden_div_par(this)">Cancel</button></td></tr><tr><td><div align="center"><button type="button" name="Submit7">down</button></div></td><td><button type="button" name="Submit3" onclick="delParameter(this)">Delete Parameter</button></td></tr></table></div></td></tr>');

})


$("option").each(function(){
if($(this).parent().attr("name")==$(this).val())
{
$(this).attr("selected","selected");
}
if($(this).parent().attr("id")==$(this).val())
{
$(this).attr("selected","selected");
}
})

$("input[type='checkbox']").each(function(){

if($(this).val()=="1")
{
$(this).attr("checked","checked");
}
})

}

你可能感兴趣的:(java中数组转换为json格式)