Convert Java String to Json
I have a list of String which I wish to convert to Json. I am using org\json\me in order to do so. However, I don't know how to continue from here. A little help will be appreciated. Thanks.
This is my code:
public class PhoneData implements JSONAble {
private Display display;
private Form mainScr;
public PhoneData() {
mainScr = new Form("Phone Data");
String imei = IDENInfo.imeiToString(IDENInfo.getIMEI());
String imsi = new String();
try{
imsi=GPRSInfo.imeiToString(SIMCardInfo.getIMSI(), false );
}catch(SIMCardException ioe){}
String majorOS = DeviceInfo.getPlatformVersion();
int content = CodeModuleManager.getModuleHandle("net_rim_bb_phone_api");
String version = CodeModuleManager.getModuleVersion(content); //DeviceInfo.getSoftwareVersion();
String modelnumber = DeviceInfo.getDeviceName(); //get modelnumber
String [] phoneData = new String[] { modelnumber = "Model开发者_运维百科 Number", majorOS = "majorOS", version = "softwareversion"
,imei = "imei", imsi = "imsi"}; // an Array
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(mainScr);
PhoneData user = new PhoneData();
}
public void fromJSON(String jsonString) {
// TODO Auto-generated method stub
}
public String toJSON() {
// TODO Auto-generated method stub
return null;
}
}
You'll have to put your data inside a Vector
, then use the new JSONArray(yourvector)
to make a JSONArray
.
Unless you specifically want to use org.json's barebones package, maybe have a look at this question.
You could use the Jettison driver with XStream to serialize / deserialize string <-> json : http://x-stream.github.io/json-tutorial.html
I had used this sometime back - there are some caveats on how the json schema should be for the parser to parse perfectly well.
精彩评论