2012-03-26 34 views
1

我是J2ME中JSON編程的新手。如何在J2ME中獲取和設置JSONObject,JSONArray

我發現Json用於像XML那樣交換數據。

我想知道從JSONtoObject在Array對象的交換,反之亦然

下面寫的代碼,我從JSON轉換爲對象,反之亦然。

但我不知道如何爲複雜的數據結構不喜歡陣列等

//應用程序加載器

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的String類型對象,然後創建的JSONObject創建JSON對象創建一個JSON對象,然後反之亦然按功能toJSON()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(); 
     } 



    } 

} 

我找到了更好的鏈接,這個問題

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

回答

4

Check this link for different JSON Data Set Sample

一個例子::: 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" } 
     ] 
} 

要檢查其有效嵌套或不是check this link (JSON Validator)

要檢查JSON Viewer

因此,這裏是代碼拿來看::

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中總是很容易處理,不用擔心。由於

+0

這是一個很好的方法謝謝 – Yatin 2012-03-26 14:09:22

+0

你還有這個罐子嗎?我試圖從源文件創建它,但我可能做錯了什麼,因爲我不能得到它的工作:S https://github.com/upictec/org.json.me – eddy 2014-05-23 23:47:58

+0

是的,請我找不到任何地方的瓶 – Axel 2014-05-24 12:43:03

0

它幾乎以同樣的方式..所有你需要的是剛剛通過數組循環...我添加標籤您的樣本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()); 
    } 
} 

輸出

99 
    Tester 
    This is JSON Data 
    eat 
    swim 
    sleep 

我希望這有助於

感謝 :)

的理解
+0

你還有罐子嗎?我試圖從源文件創建它,但我可能做錯了什麼,因爲我無法得到它的工作:S github.com/upictec/org.json.me – eddy 2014-05-24 00:04:42

+0

是的請我不能找到任何地方的罐子! – Axel 2014-05-24 12:45:58

1

在下面的鏈接 http://jimmod.com/blog/2011/09/java-me-j2me-json-implementation-for-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中定義的類,它已在數組對象作了 退房鏈接

+0

你還有罐子嗎?我試圖從源文件創建它,但我可能做錯了什麼,因爲我無法得到它的工作:S github.com/upictec/org.json.me – eddy 2014-05-23 23:53:22

+0

是的請我不能找到任何地方的罐子! – Axel 2014-05-24 12:43:23