Json 应用
根据对像生成字符串:
private String getWholesalePriceJson(List<WholesaleProductPriceDTO> priceDtolist) {
List<WholesalePriceDTO> priceList = new ArrayList<WholesalePriceDTO>();
if (priceDtolist != null) {
for (WholesaleProductPriceDTO wholesaleProductPriceDTO : priceDtolist) {
priceList.add(new WholesalePriceDTO(getWholesaleStandardPriceIdList(wholesaleProductPriceDTO),
wholesaleProductPriceDTO.getStandard()));
}
}
return JSONArray.fromObject(priceList).toString();
}
页面显示与操作:
function checkStandard(){
var YLJ = YAHOO.lang.JSON;
var priceList = YLJ.parse(document.getElementById("wholesalePrice").value);
//开始校验重复数据
var map={};
for (var i = 0; i < priceList.length; i++) {
if(priceList[i].value.length > 32) {
alert("您好,产品介格中产品规则字段长度超过32!");
return false;
}
if(map[priceList[i].value] == null) {
map[priceList[i].value] = 1;
} else {
alert("您好,产品价格中产品规则字段有重复值!");
return false;
}
}
return true;
}
再转换回去成为对象.
JSONArray array = JSONArray.fromObject(json);
return (List<WholesalePriceDTO>) JSONArray.toCollection(array, WholesalePriceDTO.class);
这样简单的应用就完成了
()
//使用JSONObject JSONArray 类
JSONArray array = new JSONArray();
JSONObject jobject = new JSONObject();
String titles = "<a href=/"http://www.163.com/">我是/"title/"</a>";
for (int n = 0; n < 5; n++){
JSONObject jo = new JSONObject();
jo.put("id",n);
jo.put("title",titles + n);
jo.put("cruser","cruser"+n);
jo.put("crdate","crdate"+n);
jo.put("udtuser","udtuser"+n);
jo.put("udtdate","udtdate"+n);
jo.put("hits","hits"+n);
jo.put("hot",true);
jo.put("ordernum",n);
jo.put("status","status"+n);
array.put(jo);
}
jobject.put("totalCount",30);
jobject.put("root",array);
System.out.println(jobject);
记得
import java.util.*;
import org.json.*;
还有try catch
(补充)
package com.longtop.ajax.json;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject ;
public class TestServlet {
public static void main(String[] args){
TestServlet ts=new TestServlet();
System.out.println(ts.dellJson());
// JSONObject jo=JSONObject .fromObject(ts.dellJson());
try{
JSONObject jo=new JSONObject (ts.dellJson());
JSONArray asonArray = jo.getJSONArray("addressbood");
Object o = asonArray.get(1);
JSONObject jo1 = (JSONObject )o;
System.out.println(jo1.toString());
//JSONArray array=jo1.getJSONArray("address");
System.out.println(jo1.get("address"));
JSONArray asonArray1 = jo1.getJSONArray("address");
JSONObject jo11 = (JSONObject )asonArray1.get(0);
System.out.println(jo11.getString("city"));
}catch(JSONException a){
a.printStackTrace();
}
}
public String dellJson(){
String str="";
Map map=new HashMap();
Address maryLebow = new Address("5 Main Street","San Diego, CA","91912","619-332-3452","664-223-4667");
map.put("Mary Lebow",maryLebow);
Address amySmith = new Address("25 H Street","Los Angeles, CA","95212","660-332-3452","541-223-4667");
map.put("Sally May",amySmith);
Address johnKim = new Address("2343 Sugarland Drive","Houston, TX","55212","554-332-3412","461-223-4667");
map.put("John Kim",johnKim);
Address richardThorn = new Address("14 68th Street","New York, NY","12452","212-132-6182","161-923-4001");
map.put("Richard Thorn",richardThorn);
Iterator it=map.entrySet().iterator();
JSONArray ja=new JSONArray();
while(it.hasNext()){
Map.Entry e=(Map.Entry)it.next();
String name=(String)e.getKey();
Address add=(Address)e.getValue();
String city=add.getCity();
String street=add.getStreet();
String zip=add.getZip();
String phone1=add.getPhoneNumber1();
String phone2=add.getPhoneNumber2();
try{
JSONObject jbAddress=new JSONObject ();
jbAddress.append("street",street);
jbAddress.append("city",city);
jbAddress.append("zip",zip);
JSONArray jaPhone=new JSONArray();
jaPhone.put( phone1);
jaPhone.put( phone2);
JSONObject jbAddressBook=new JSONObject ();
jbAddressBook.append("name",name);
jbAddressBook.append("address",jbAddress);
jbAddressBook.append("phoneNumbers",jaPhone);
ja.put(jbAddressBook);
}catch(JSONException a){
a.printStackTrace();
}
}
try{
str=new JSONObject ().put("addressbood",ja).toString();
}catch(JSONException a){
a.printStackTrace();
}
return str;
}
}