第一个节目
package bll.sap;
import java.net.MalformedURLException;
import utility.PropertyUtility;
public class GetActiveData
{
public static void main(String[] args) {
String sapURL = "";
String transactionSapURL="";
try {
sapURL = PropertyUtility.getSapURL();
transactionSapURL = PropertyUtility.getTransactionSapURL();
//Get Summary Data
//SapDataSync sapDataSync = new SapDataSync();
SapDataSync sapDataSync = new SapDataSync();
//sapDataSync.readTransactionJsonFromUrl(sapURL+"E13F42EC5E38");
sapDataSync.readTransactionJsonFromUrl("http://localhost/uatpw/ActiveTransaction?isx=E13F5AFA45CE");
}
catch(MalformedURLException me)
{
me.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
第二个节目
package bll.sap;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import utility.Utility;
import com.google.code.morphia.Datastore;
import com.sun.jndi.toolkit.url.Uri;
import dal.GetMorphiaDB;
public class SapDataSync
{
private void saveSapTransaction(List<TransactionUnit> sapTransaction){
GetMorphiaDB morphia;
try {
morphia = GetMorphiaDB.getInstance();
Datastore ds = morphia.getDs();
ds.save(sapTransaction);
}catch (Exception e) {
e.printStackTrace();
}
}
private void createSapTransaction(String transactionJson)
{
JSONObject jsonObj = JSONObject.fromObject(transactionJson);
JSONArray transactionUnits = jsonObj.getJSONArray("TRANSACTION");
//System.out.println("transactionUnits="+transactionUnits);
List<ActiveUnit> transactionList = new ArrayList<ActiveUnit>();
for(int i = 0; i < transactionUnits.size() ; i++)
{
JSONObject jsn = transactionUnits.getJSONObject(i);
ActiveUnit transactionUnit = new ActiveUnit
(
jsn.getString("listEditions"),
jsn.getString("listPackage"),
jsn.getString("referenceID")
);
//System.out.println("transactionUnit ="+transactionUnit);
transactionList.add(transactionUnit);
//System.out.println("transactionList ="+transactionList);
}
//System.out.println(transactionList.size());
if (transactionList.size() > 0) {
//saveSapTransaction(transactionList);
}
}
public void readTransactionJsonFromUrl(String url) throws IOException, JSONException
{
InputStream is = new URL(url).openStream();
try
{
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1)
{
sb.append((char) cp);
}
createSapTransaction(sb.toString());
}
finally
{
is.close();
}
}
}
第三节目
package bll.sap;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
@Entity("SapTransaction")
public class ActiveUnit implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String listEditions;
private String listPackage;
private String referenceID;
private List<ActiveUnit> listEdition;
public ActiveUnit() {
}
public ActiveUnit(String listEdtions, String listPackage,String referenceID) {
this.listEditions = listEdtions;
this.listPackage = listPackage;
this.referenceID = referenceID;
}
public String getreferenceID() {
return referenceID;
}
public void setreferenceID(String referenceID) {
this.referenceID = referenceID;
}
public String getListEditions() {
return listEditions;
}
public void setListEditions(String listEditions) {
this.listEditions = listEditions;
}
public String getListPackage() {
return listPackage;
}
public void setListPackage(String listPackage) {
this.listPackage = listPackage;
}
@Override
public String toString()
{
String unit = "{ " +"listEditions: " + this.listEditions+
",listPackage: "+ this.listPackage+
",referenceID: " + this.referenceID+
"}";
return unit;
}
}
运行第一个程序后 我从该URL获取JSON格式的数据,如下所示。
{
"TRANSACTION":[
{
"listEditions": [
{
"adRoute":"B",
"listInsertion":[
{
"color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
}
],
listPackage:
[
{
listEditions: [
{
"adRoute":"B",
"listInsertion":[
{
"color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
},
{
"adRoute":"A",
"listInsertion":[
{ "color":0,"colorType":"All Colour","fromDate":"26/06/2013","height":25,"page":2,"pagePosition":2,"pagePositionType":"Regular Page","size":823,"sizeDimention":"32.9x25","toDate":"26/06/2013","width":32.9,"pubDate":""
}
]
}
]
}],
"referenceID": "E13F42EC5E38"
}
]
}
现在的问题是如何从内部具有内部数组的JSON数据中读取所有键和值。 我可以从TRANSACTION数组中读取数据。 但进一步如果我想读取listEditions数组中的数据和listInditions数组中的数组listInsertion,那么它会给我带来问题。 然而,我尝试了一些方法,但静脉。 请帮忙。 当前代码仅用于从TRANSACTION Array读取数据。 我没有得到如何编码从其他数组读取数据。 我试着读它,但它说它不是一个数组,它是一个字符串。
答案 0 :(得分:0)
如果你有一个可以使用JSON字符串构建的支持对象模型,你可以使用google的Gson API,它将无缝地解析JSON字符串并为你创建对象。