如何获取和设置的JSONObject,JSONArray在J2ME [英] How to get and set JSONObject , JSONArray in J2ME
问题描述
我是新来的J2ME编程的JSON
I am new to JSON programming in J2ME.
我发现,JSON是用来很像XML的数据交换。
I discovered that Json is used to exchange data much like XML.
我想了解一下,从JSONtoObject在Array对象交换,反之亦然。
I want to know about the exchange in Array object from JSONtoObject and vice versa
下面写为code,其中我从JSON转换为对象,反之亦然。
Below written is code where I convert from JSON to Object and vice versa.
但我不知道如何像数组等复杂的数据结构做的。
But I don't know about how to do for complex data structure like arrays etc.
//应用程序加载器
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class AppLoader extends MIDlet {
public AppLoader() {
// TODO Auto-generated constructor stub
// Converting Object to JSON
UserData data=new UserData();
data.setId(10);
data.setName("Yatin");
data.setDescription("Testing JSON in J2ME");
System.out.println("Convert to JSON"+data.toJSON());
//Convert JSON to Object
String sample="{\"id\":99,\"name\":\"Tester\",\"description\":\"This is JSON Data\"}";
UserData data2=new UserData();
data2.fromJSON(sample);
System.out.println("Convert from JSON "+data2);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
}
在这个类我创建getter和setter方法的对象的字符串类型,然后创建的JSONObject创建一个JSON对象创建一个JSON对象,然后反之亦然按功能的toJSON()
和 fromJSON()
In this class I created getters and setters for the String type of objects and then created JsonObject to create a JSON object to create a JSON object and then vice versa as per functions toJSON()
and fromJSON()
//用户数据类
import org.json.me.JSONException;
import org.json.me.JSONObject;
public class UserData {
private int id;
private String name;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String toString()
{
return getId()+"-"+getName()+"-"+getDescription();
}
public String toJSON() {
// TODO Auto-generated method stub
JSONObject inner=new JSONObject();
try {
inner.put("id",getId());
inner.put("description", getDescription());
inner.put("name", getName());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inner.toString();
}
public void fromJSON(String jsonString) {
// TODO Auto-generated method stub
try {
JSONObject json=new JSONObject(jsonString);
setId(json.getInt("id"));
setDescription(json.getString("description"));
setName(json.getString("name"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我发现了一个更好的链接,这个问题
i found a better link for this question
的http://jimmod.com/blog/2011/09/java-me-j2me-json-implementation-for-array-object/
推荐答案
您的理解一个例子::: JSON字符串嵌套使用数组
One Example for your Understanding::: JSON String Nested with Arrays
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
要检查其有效还是无效检查此链接(JSON验证)
To check its valid or not check this link (JSON Validator)
要检查 JSON查看器
所以这里是code拿来看::
So Here is code take look::
String json = "{\"id\":\"0001\",\"type\":\"donut\",\"name\":\"Cake\""
+ ",\"ppu\":0.55,\"batters\":{\"batter\":["
+ "{\"id\":\"1001\",\"type\":\"Regular\"},{\"id\":\"1002\","
+ "\"type\":\"Chocolate\"},{\"id\":\"1003\","
+ "\"type\": \"Blueberry\" },{ \"id\": \"1004\", "
+ "\"type\": \"Devil's Food\" } ] },"
+ " \"topping\":["
+ "{ \"id\": \"5001\", \"type\": \"None\" },"
+ "{ \"id\": \"5002\", \"type\": \"Glazed\" },"
+ "{ \"id\": \"5005\", \"type\": \"Sugar\" },"
+ "{ \"id\": \"5007\", \"type\": \"Powdered Sugar\" },"
+ " { \"id\": \"5006\", \"type\": \"Chocolate with Sprinkles\" },"
+ "{ \"id\": \"5003\", \"type\": \"Chocolate\" },"
+ "{ \"id\": \"5004\", \"type\": \"Maple\" }]}";
try {
JSONObject root = new JSONObject(json);
String id = root.getString("id");
double dd = root.getDouble("ppu");
System.out.println(""+id);
System.out.println(""+dd);
JSONObject batters=new JSONObject(root.getString("batters"));
JSONArray batter=new JSONArray(batters.getString("batter"));
for(int j=0;j<batter.length();j++){
JSONObject navgt_batter=new JSONObject(batter.getString(j));
String id_batter= navgt_batter.getString("id");
String type_batter=navgt_batter.getString("type");
System.out.println(""+id+" "+type_batter);
}
JSONArray topping=root.getJSONArray("topping");
for(int k=0;k<topping.length();k++){
JSONObject navgt_batter=new JSONObject(topping.getString(k));
String id_top =navgt_batter.getString("id");
String type_top=navgt_batter.getString("type");
System.out.println(""+id_top+" "+type_top);
}
} catch (JSONException ex) {
ex.printStackTrace();
}
您可以使用相同的概念来设置和放大器;获取数据像上面你没有。复杂的数据结构总是件容易的JSON处理,不要担心。谢谢
You can use your same concept to set & get data like above you did. complex data structure always easy to handle in JSON, don't worry about it. Thanks
这篇关于如何获取和设置的JSONObject,JSONArray在J2ME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!