2011-09-08 12 views
21

क्या सीरियलाइजेशन में JSON का उपयोग करने के एंड्रॉइड के लिए कोई आसान उदाहरण है?एंड्रॉइड पर जेएसओएन - सीरियलाइजेशन

धन्यवाद

+1

क्या आप का मतलब है इस http://blog.brianbuikema.com/2010/04/android-how-to-deserialize-both-xml-and-json/ –

+0

ऐसा नहीं है कि हो सकता है , धन्यवाद – Waypoint

उत्तर

39

हम उस के लिए gson पुस्तकालय का उपयोग करें। क्रमबद्धता

new Gson().toJson(obj) 

और अक्रमांकन के लिए बुला के रूप में सरल है,

new Gson().fromJson(jsonStr, MyClass.class); 
18

तुम सिर्फ करने के लिए अपने Android परियोजना में एक और पुस्तकालय का उपयोग कर से बचना चाहते हैं (डी) को क्रमानुसार JSON, आप के रूप में कोड निम्नलिखित उपयोग Cau मैं करता हूँ।

JSONObject json = new JSONObject(); 
json.put("key", "value"); 
// ... 
// "serialize" 
Bundle bundle = new Bundle(); 
bundle.putString("json", json.toString()); 

क्रमानुसार करने और

Bundle bundle = getBundleFromIntentOrWhaterver(); 
JSONObject json = null; 
try { 
    json = new JSONObject(bundle.getString("json")); 
    String key = json.getString("key"); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

सादर deserialize करने के लिए, मार्टिन

-1
protected void onPostExecute(String results) { 
     if (results!=null) { 
      try { 
       Tec tec_m=new Tec(); 

       tec_m=new Gson().fromJson(results, Technician.class); 

       ((AndroidActivity)activity).setData(tec_m); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
0

के लिए एक सरल पुस्तकालय है (डी) को क्रमानुसार JSON, एंड्रॉयड खुद json के साथ संगत पुस्तकालय।

// deserialize a java bean to json object 
JSONObject studentJson = JsonDeer.toJson(student); 
// serialize a java bean from json object 
Student student1 = JsonDeer.fromJson(studentJson,Student.class); 

library address

संबंधित मुद्दे