Comment faire pour obtenir et définir JSONObject , JSONArray en J2ME

Je suis nouveau sur le JSON programmation J2ME.

J'ai découvert que Json est utilisé pour échanger des données comme XML.

Je veux savoir à propos de l'échange dans la gamme pour objet de JSONtoObject et vice versa

Écrit ci-dessous est le code où je convertir de JSON à l'Objet, et vice versa.

Mais je ne sais pas comment faire pour la structure de données complexe, comme des tableaux, etc.

//App Loader

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

    }

}

Dans cette classe que j'ai créée getters et setters pour la Chaîne de type d'objets, puis créé JsonObject pour créer un objet JSON pour créer un objet JSON, puis vice-versa, comme par fonctions toJSON() et fromJSON()

//Les Données de l'utilisateur de la classe

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();
}
}
}

j'ai trouvé un meilleur lien pour cette question

http://jimmod.com/blog/2011/09/java-me-j2me-json-implementation-for-array-object/

InformationsquelleAutor Yatin | 2012-03-26