2015-09-08 15 views
6

मैं जीसन का उपयोग कर जेसन कैसे पार्स कर सकता हूं? मेरे पास एकाधिक ऑब्जेक्ट प्रकारों के साथ एक जेसन सरणी है, और मुझे नहीं पता, इस संरचना को बचाने के लिए मुझे किस प्रकार की वस्तु बनाने की आवश्यकता है। मैं जेसन डेटा प्रारूप नहीं बदल सकता (मैं सर्वर को नियंत्रित नहीं करता)। क्या मैं इस जेसन सरणी को जीसन या अन्य लाइब्रेरी पार्स का उपयोग कर सकता हूं, मुझे कैसे करना चाहिए?जीसन द्वारा एकाधिक ऑब्जेक्ट्स के साथ कैसे पार्स जेसन सरणी?

[ 
    { 
    "type": 1, 
    "object": { 
     "title1": "title1", 
     "title2": "title2" 
    } 
    }, 
    { 
    "type": 2, 
    "object": [ 
     "string", 
     "string", 
     "string" 
    ] 
    }, 
    { 
    "type": 3, 
    "object": [ 
     { 
     "url": "url", 
     "text": "text", 
     "width": 600, 
     "height": 600 
     }, 
     { 
     "url": "url", 
     "text": "text", 
     "width": 600, 
     "height": 600 
     } 
    ] 
    }, 
    { 
    "type": 4, 
    "object": { 
     "id": 337203, 
     "type": 1, 
     "city": "1" 
    } 
    } 
] 
+0

हाय! क्या आपने अभी तक अपना जवाब पढ़ा है और कोशिश की है? – BNK

उत्तर

5

यह जेसन संरचना स्वाभाविक रूप से जीसन-असभ्य है। यानी आप इसे जावा में स्पष्ट रूप से मॉडल नहीं कर सकते क्योंकि "ऑब्जेक्ट" कुंजी गतिशील प्रकार को संदर्भित करती है।

String json = "{ ... json string ... }"; 
Gson gson = new Gson(); 
Models model = gson.fromJson(json, Models.class); 


for (Models.Container container : model) { 

    String innerJson = gson.toJson(container.object); 

    switch(container.type){ 
     case 1: 
      Models.Type1Object type1Object = gson.fromJson(innerJson, Models.Type1Object.class); 
      // do something with type 1 object...         
      break; 
     case 2: 
      String[] type2Object = gson.fromJson(innerJson, String[].class); 
      // do something with type 2 object... 
      break; 
     case 3: 
      Models.Type3Object[] type3Object = gson.fromJson(innerJson, Models.Type3Object[].class); 
      // do something with type 3 object... 
      break; 
     case 4: 
      Models.Type4Object type4Object = gson.fromJson(innerJson, Models.Type4Object.class); 
      // do something with type 4 object... 
      break; 

    } 
} 

अंत में सबसे अच्छा समाधान json संरचना प्राप्त करने के लिए है:

public class Models extends ArrayList<Models.Container> { 

    public class Container { 
     public int type; 
     public Object object; 
    } 

    public class Type1Object { 
     public String title1; 
     public String title2; 
    } 

    public class Type3Object { 
     public String url; 
     public String text; 
     public int width; 
     public int height; 
    } 

    public class Type4Object { 
     public int id; 
     public int type; 
     public int city; 
    } 

} 

फिर प्रकार पर कुछ अजीब स्विच और वस्तु क्षेत्र कार्य करें: सबसे अच्छा आप इस संरचना के साथ क्या कर सकते हैं मॉडल यह इतना की तरह है जावा के साथ कुछ और संगत में बदल गया।

उदा:

[ 
    { 
    "type": 1, 
    "type1Object": { 
     "title1": "title1", 
     "title2": "title2" 
    } 
    }, 
    { 
    "type": 2, 
    "type2Object": [ 
     "string", 
     "string", 
     "string" 
    ] 
    }, 
    { 
    "type": 3, 
    "type3Object": [ 
     { 
     "url": "url", 
     "text": "text", 
     "width": 600, 
     "height": 600 
     }, 
     { 
     "url": "url", 
     "text": "text", 
     "width": 600, 
     "height": 600 
     } 
    ] 
    }, 
    { 
    "type": 4, 
    "type4Object": { 
     "id": 337203, 
     "type": 1, 
     "city": "1" 
    } 
    } 
] 
+0

धन्यवाद के रूप में अलग नाम होना चाहिए, यह मेरे लिए काम करता है। – xiangmao

1

आप बहुत आसानी से अपने मॉडल कक्षा में तरीकों सेट कर सकते हैं:

यह json कोड ब्लॉक है। बस एक स्ट्रिंगरक्वेट बनाएं।

List<YourModelClass> inpList; 
StringRequest greq = new StringRequest(Request.Method.POST, yourURL, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       try { 
         Log.d("response array===> ", response.toString()); 

         Type type = new TypeToken<List<YourModelClass>>(){}.getType(); 
         inpList = new Gson().fromJson(response, type); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       error.printStackTrace(); 

      } 
     }){ 
      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 
       Map<String, String> params = new HashMap<String, String>(); 
       //return params back to server, if any 
      } 
     }; 

     yourVolley.getRequestQueue().add(greq); 

मैं this का इस्तेमाल किया है json आप से अपने मॉडल वर्ग उत्पन्न करने के लिए: नीचे एक टुकड़ा है। आपका मॉडल वर्ग कुछ इस तरह दिखेगा:

package com.example; 

import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 

@Generated("org.jsonschema2pojo") 
public class YourModelClass { 

@Expose 
private Integer type; 
@Expose 
private Object object; 

/** 
* 
* @return 
* The type 
*/ 
public Integer getType() { 
return type; 
} 

/** 
* 
* @param type 
* The type 
*/ 
public void setType(Integer type) { 
this.type = type; 
} 

/** 
* 
* @return 
* The object 
*/ 
public Object getObject() { 
return object; 
} 

/** 
* 
* @param object 
* The object 
*/ 
public void setObject(Object object) { 
this.object = object; 
} 

} 
-----------------------------------com.example.Object.java----------------------------------- 

package com.example; 

import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 

@Generated("org.jsonschema2pojo") 
public class Object { 

@Expose 
private Integer id; 
@Expose 
private Integer type; 
@Expose 
private String city; 

/** 
* 
* @return 
* The id 
*/ 
public Integer getId() { 
return id; 
} 

/** 
* 
* @param id 
* The id 
*/ 
public void setId(Integer id) { 
this.id = id; 
} 

/** 
* 
* @return 
* The type 
*/ 
public Integer getType() { 
return type; 
} 

/** 
* 
* @param type 
* The type 
*/ 
public void setType(Integer type) { 
this.type = type; 
} 

/** 
* 
* @return 
* The city 
*/ 
public String getCity() { 
return city; 
} 

/** 
* 
* @param city 
* The city 
*/ 
public void setCity(String city) { 
this.city = city; 
} 

} 
+0

लेकिन ऑब्जेक्ट में चार प्रकार हैं, इसके साथ कैसे निपटें – rainash

+0

आप मॉडल क्लास को बदल सकते हैं और ऑब्जेक्ट को "ऑब्जेक्ट" श्रेणी की सूची के रूप में परिभाषित कर सकते हैं। मैं, आप एक और वर्ग वस्तु बना देंगे और अपने गेटर्स और सेटर्स को अलग से परिभाषित करेंगे। लेकिन हां, मुझे लगता है कि ऑब्जेक्ट वैल्यू के रूप में ऑब्जेक्ट वैल्यू से ऑब्जेक्ट वैल्यू –

0

आप जारी के लिए, मैं निम्नलिखित समाधान का सुझाव:

अपने भीतरी वस्तु एक json वस्तु या एक json सरणी हो सकता है क्योंकि, इसलिए मैं का उपयोग करेगा gson की एक सामान्य JsonElement

public class CustomJson { 
    private int type; 
    private JsonElement object; 
} 
(आप अपनी परियोजना में इसे Ctrl-B दबाने या here चर्चा करते हुए द्वारा इस वर्ग के बारे में अधिक जानकारी प्राप्त कर सकते हैं)

फिर अपने गतिविधि, Gson का उपयोग करने में पार्स करने के लिए:

Gson gson = new Gson(); 
CustomJson[] customJson = gson.fromJson(jsonString, CustomJson[].class); 

क्योंकि com.google.gson.JsonElement हो सकता है एक JsonObject (नहीं JSONObject) या एक JsonArray (JSONArray नहीं), तो आपको JSONObject और JSONArray कन्वर्ट करने के लिए चाहते हैं, तो , निम्नलिखित कोड का उपयोग करें:

 if (customJson!= null && customJson.length > 0) { 
      for (CustomJson aCustomJson : customJson) { 
       JsonElement jsonElement = aCustomJson.object; 
       if (jsonElement instanceof JsonObject) { 
        try { 
         JSONObject jsonObject = new JSONObject(jsonElement.toString()); 
         Log.i(LOG_TAG, jsonObject.toString()); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } else if (jsonElement instanceof JsonArray) { 
        try { 
         JSONArray jsonArray = new JSONArray(jsonElement.toString()); 
         Log.i(LOG_TAG, jsonArray.toString()); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     } 

यहां मेरे प्रोजेक्ट में लॉगकैट परिणाम हैं:

09-08 15:41:48.024 6202-6202/com.example.jsonparse I/JSONParse﹕ {"title1":"title1","title2":"title2"} 
09-08 15:41:51.458 6202-6202/com.example.jsonparse I/JSONParse﹕ ["string","string","string"] 
09-08 15:41:51.458 6202-6202/com.example.jsonparse I/JSONParse﹕ [{"text":"text","url":"url","height":600,"width":600},{"text":"text","url":"url","height":600,"width":600}] 
09-08 15:41:51.458 6202-6202/com.example.jsonparse I/JSONParse﹕ {"type":1,"id":337203,"city":"1"} 

उम्मीद है कि इससे मदद मिलती है!

1

यह मूल पोस्टर के लिए थोड़ी देर हो चुकी हो सकता है लेकिन उम्मीद है कि यह किसी और में मदद मिलेगी।

मैं में Gson का उपयोग कर रहा हूं। मैंने देखा है कि हर कोई कस्टम कक्षाओं और लंबे रास्ते के दौर समाधान का उपयोग करता है। मेरा मूल है।

मेरे पास कई अलग-अलग ऑब्जेक्ट प्रकारों (मेरे डेटाबेस के लिए मॉडल) का एक ऐरेलिस्ट है - प्रोफ़ाइल उनमें से एक है।

{"profile": 
    {"name":"Josh", 
    "position":"Programmer", 
    "profile_id":1, 
    "profile_image_id":10, 
    "user_id":1472934469 
    }, 
"user": 
    {"email":"[email protected]", 
    "phone_numbers":[], 
    "user_id":1, 
    "user_type_id":1 
    }, 
"follower": 
    {"follower_id":3, 
    "following_date":1.4729345E9, 
    "referred_by_id":2, 
    "user_from_id":1, 
    "user_to_id":2 
    }, 
"media": 
    {"link":"uploads/profiles/profile-photos/originals/1-G9FSkRCzikP4QFY.png", 
    "media_description":"", 
    "media_id":10, 
    "media_name":"", 
    "media_slug":"", 
    "medium_link":"uploads/profiles/profile-photos/thumbs-medium/1-G9FSkRCzikP4QFY.png", 
    "thumbnail_link":"uploads/profiles/profile-photos/thumbs-small/1-G9FSkRCzikP4QFY.png", 
    "uploader_id":1 
    } 
} 

अब मैं Gson वस्तु बनाने:: मैं जो रिटर्न mContactList.get(i) का उपयोग कर आइटम मिल अब बजाय

Gson gson = new Gson(); 
// this creates the JSON string you see above with all of the objects 
String str_obj = new Gson().toJson(mContactList.get(i)); 

एक कस्टम वर्ग बनाने का - बस के माध्यम से एक JsonObject के रूप में निम्नलिखित कोड का उपयोग कर पारित:

JsonObject obj = gson.fromJson(str_obj, JsonObject.class); 

और अब, आप तो जैसे वस्तु कॉल कर सकते हैं:

0,123,
JsonObject profile = obj.getAsJsonObject("profile"); 
संबंधित मुद्दे