Java 如何在J2ME中获取和设置JSONObject、JSONArray
我是J2ME中JSON编程的新手 我发现Json与XML非常相似,用于交换数据 我想从JSONtoObject了解数组中的交换对象,反之亦然 下面是我将JSON转换为Object的代码,反之亦然 但我不知道如何处理复杂的数据结构,如数组等 //应用程序加载器Java 如何在J2ME中获取和设置JSONObject、JSONArray,java,arrays,json,java-me,midp,Java,Arrays,Json,Java Me,Midp,我是J2ME中JSON编程的新手 我发现Json与XML非常相似,用于交换数据 我想从JSONtoObject了解数组中的交换对象,反之亦然 下面是我将JSON转换为Object的代码,反之亦然 但我不知道如何处理复杂的数据结构,如数组等 //应用程序加载器 import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class App
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来创建JSON对象,反之亦然
//用户数据类
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();
}
}
}
我找到了这个问题更好的链接
也差不多。。你所需要的只是在数组中循环。。。我向示例JSON数据添加了标记
String sample = "{\"id\":99,\"name\":\"Tester\",\"description\":\"This is JSON Data\",\"tags\":[\"eat\",\"swim\",\"sleep\"]}";
try {
JSONObject objSample = new JSONObject(sample);
JSONArray array = new JSONArray(objSample.getJSONArray("tags").toString());
System.out.println(objSample.get("id").toString());
System.out.println(objSample.get("name").toString());
System.out.println(objSample.get("description").toString());
for (int i = 0; i < array.length(); i++) {
System.out.println(array.get(i).toString());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
我希望这有帮助
谢谢
:也一样。。你所需要的只是在数组中循环。。。我向示例JSON数据添加了标记
String sample = "{\"id\":99,\"name\":\"Tester\",\"description\":\"This is JSON Data\",\"tags\":[\"eat\",\"swim\",\"sleep\"]}";
try {
JSONObject objSample = new JSONObject(sample);
JSONArray array = new JSONArray(objSample.getJSONArray("tags").toString());
System.out.println(objSample.get("id").toString());
System.out.println(objSample.get("name").toString());
System.out.println(objSample.get("description").toString());
for (int i = 0; i < array.length(); i++) {
System.out.println(array.get(i).toString());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
我希望这有帮助
谢谢
:
一个示例供您理解:::与数组嵌套的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" }
]
}
检查它是否有效
查证
下面是代码,请看:
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中总是很容易处理,不用担心。谢谢
一个示例供您理解:::与数组嵌套的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" }
]
}
检查它是否有效
查证
下面是代码,请看:
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中总是很容易处理,不用担心。在下面的链接中感谢
他们已经解释了如何使用JSONArray
public void fromJSON(String jsonString) {
try {
JSONObject json = new JSONObject(jsonString);
setApi_status(json.getString("api_status"));
JSONArray jsonArray = json.getJSONArray("threads");
int total = jsonArray.length();
ThreadData[] threads = new ThreadData[total];
for (int i=0;i<total;i++) {
String threadsJSON = jsonArray.getString(i);
threads[i] = new ThreadData();
threads[i].fromJSON(threadsJSON);
}
setThreads(threads);
} catch (JSONException ex) {
ex.printStackTrace();
}
}
public String toJSON() {
JSONObject inner = new JSONObject();
try {
inner.put("api_status", getApi_status());
JSONArray jsonArray = new JSONArray();
ThreadData[] threads = getThreads();
for (int i=0;i<threads.length;i++) {
jsonArray.put(threads[i].toJSON());
}
inner.put("threads", jsonArray);
} catch (JSONException ex) {
ex.printStackTrace();
}
return inner.toString();
}
其中Threaddata是为JSONObject定义的类,它是在array object中创建的
查看下面链接中的链接
他们已经解释了如何使用JSONArray
public void fromJSON(String jsonString) {
try {
JSONObject json = new JSONObject(jsonString);
setApi_status(json.getString("api_status"));
JSONArray jsonArray = json.getJSONArray("threads");
int total = jsonArray.length();
ThreadData[] threads = new ThreadData[total];
for (int i=0;i<total;i++) {
String threadsJSON = jsonArray.getString(i);
threads[i] = new ThreadData();
threads[i].fromJSON(threadsJSON);
}
setThreads(threads);
} catch (JSONException ex) {
ex.printStackTrace();
}
}
public String toJSON() {
JSONObject inner = new JSONObject();
try {
inner.put("api_status", getApi_status());
JSONArray jsonArray = new JSONArray();
ThreadData[] threads = getThreads();
for (int i=0;i<threads.length;i++) {
jsonArray.put(threads[i].toJSON());
}
inner.put("threads", jsonArray);
} catch (JSONException ex) {
ex.printStackTrace();
}
return inner.toString();
}
其中Threaddata是为JSONObject定义的类,它是在array object中创建的
查看链接这是一个很棒的方法,谢谢你还有罐子吗??我试图从源文件创建它,但我可能做错了什么,因为我无法让它工作:是的,请我在任何地方都找不到这个罐子!!这是一个很棒的方法谢谢你还有罐子吗??我试图从源文件创建它,但我可能做错了什么,因为我无法让它工作:是的,请我在任何地方都找不到这个罐子!!你还有罐子吗??我试着从源文件创建它,但我可能做错了什么,因为我无法让它工作:S github.com/upictec/org.json.meYes我在任何地方都找不到jar!!你还有罐子吗??我试着从源文件创建它,但我可能做错了什么,因为我无法让它工作:S github.com/upictec/org.json.meYes我在任何地方都找不到jar!!你还有罐子吗??我试着从源文件创建它,但我可能做错了什么,因为我无法让它工作:S github.com/upictec/org.json.meYes我在任何地方都找不到jar!!你还有罐子吗??我试着从源文件创建它,但我可能做错了什么,因为我无法让它工作:S github.com/upictec/org.json.meYes我在任何地方都找不到jar!!