2017-04-07 71 views
-2
 { 
    "DATATABLE": [ 
     { 
     "ENTITYID": "2", 
     "USERID": "5897", 
     "ORGID": "P01", 
     "COMPID": "A0002", 
     "IP": "0", 
     "INDENT_NO": "0", 
     "SERIAL": "1", 
     "DOC_DT": "06/04/2017", 
     "ITM_CD": "100000397", 
     "QTY": "6", 
     "RATE": "9", 
     "FROM_LOC": "0", 
     "REMARK": "Re", 
     "REQ_DT": "06/04/2017", 
     "ADD_SPEC": "Ass", 
     "PROJECT": "0", 
     "IND_TYPE": "R", 
     "IND_CAT": "REV", 
     "PURPOSE": "P" 
    } 
       ], 
    "Mode": "I" 
    } 

如何使用上述代碼在java中創建Json對象 請給我代碼。我在堆棧溢出搜索,但我沒有得到相同的JSON格式的JSON對象代碼如何在bellow中創建jsonObject在java中的結構

+0

請張貼相關的代碼 – Pehlaj

+0

使用這個工具:http://www.jsonschema2pojo.org/ –

+0

可能[使用Volley發送帶有JSON數據的POST請求]的副本(http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley) –

回答

0

這是示例代碼片段。

class DATATABLE{ 
    String ENTITYID; 
    .... define all fileds here. 
} 

class Data{ 
    List <'DATATABLE> list; 
    String Mode; 
} 
+1

歡迎來到Stack Overflow!雖然此代碼可能會回答這個問題,但提供關於_how_和/或_why_的附加[上下文](https://meta.stackexchange.com/q/114762)可以解決問題,從而提高答案的長期價值。請記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請[編輯](http://stackoverflow.com/posts/43272560/edit)您的答案添加一個解釋,並指出適用的限制和假設。它也不會提到爲什麼這個答案比其他答案更合適。 –

0

創建JSON PARAM喜歡它,並將其發送PARAM與凌空要求

JSONObject paramObj = new JSONObject(); 
JSONArray dataArray = new JSONArray(); 
JSONObject Obj = new JSONObject(); 
Obj.put("ENTITYID", "2"); 
Obj.put("USERID", "2"); 

// and so on.... 

// Now put obj into array 
dataArray.put(0, Obj); 

// put array and last value into paramObj 
paramObj.put("DATATABLE", dataArray); 
paramObj.put("Mode", "I"); 

發送 「paramObj」 的JSONObject與凌空要求PARAM。

和更多關於如何使用JSON參數排氣可以休耕this教程這將是對你有所幫助。

0

我已經用volley來解析上面的json格式。 爲下面的代碼解析您的網址。

清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.theme.androidvolley"> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <application 
     android:name="AppController" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

AppController.Java

** *創建者根本上17年7月4日。 */

public class AppController extends Application { 
    public static final String TAG = AppController.class.getSimpleName(); 
    private RequestQueue mRequestQueue; 
    private static AppController mInstance; 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mInstance = this; 
    } 
    public static synchronized AppController getInstance() { 
     return mInstance; 
    } 
    public RequestQueue getRequestQueue() { 
     if (mRequestQueue == null) { 
      mRequestQueue = Volley.newRequestQueue(getApplicationContext()); 
     } 
     return mRequestQueue; 
    } 
    public <T> void addToRequestQueue(Request<T> req, String tag) { 
     req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); 
     getRequestQueue().add(req); 
    } 
    public <T> void addToRequestQueue(Request<T> req) { 
     req.setTag(TAG); 
     getRequestQueue().add(req); 
    } 
    public void cancelPendingRequests(Object tag) { 
     if (mRequestQueue != null) { 
      mRequestQueue.cancelAll(tag); 
     } 
    } 
} 

MainActivity.java:

public class MainActivity extends AppCompatActivity { 
    // json object response url 
    //Parse your url 
    private String urlJsonObj = " // Parse your url"; 
    private static String TAG = MainActivity.class.getSimpleName(); 
    private Button btnMakeObjectRequest; 
    // Progress dialog 
    private ProgressDialog pDialog; 
    private TextView txtResponse; 
    // temporary string to show the parsed response 
    private String jsonResponse; 
    Activity mActivity; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mActivity = MainActivity.this; 
     btnMakeObjectRequest = (Button) findViewById(R.id.btnObjRequest); 
     txtResponse = (TextView) findViewById(R.id.txtResponse); 
     pDialog = new ProgressDialog(this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     btnMakeObjectRequest.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // making json object request 
       makeJsonObjectRequest(); 
      } 
     }); 
    } 
    /** 
    * Method to make json object request where json response starts wtih { 
    */ 
    private void makeJsonObjectRequest() { 
     showpDialog(); 

     JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 
       urlJsonObj, null, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       Log.d(TAG, response.toString()); 
       try { 
        // Getting JSON Array node 
        JSONArray contacts = response.getJSONArray("DATATABLE"); 
        // looping through All Contacts 
        for (int i = 0; i < contacts.length(); i++) { 
         JSONObject c = contacts.getJSONObject(i); 
         String ENTITYID = c.getString("ENTITYID"); 
         String USERID = c.getString("USERID"); 
         String ORGID = c.getString("ORGID"); 
         String COMPID = c.getString("COMPID"); 
         String IP = c.getString("IP"); 
         String SERIAL = c.getString("SERIAL"); 
         String DOC_DT = c.getString("DOC_DT"); 
         String ITM_CD = c.getString("ITM_CD"); 
         jsonResponse = ""; 
         jsonResponse += "ENTITYID: " + ENTITYID + "\n\n"; 
         jsonResponse += "USERID: " + USERID + "\n\n"; 
         jsonResponse += "ORGID: " + ORGID + "\n\n"; 
         jsonResponse += "COMPID: " + COMPID + "\n\n"; 
         jsonResponse += "IP: " + IP + "\n\n"; 
         jsonResponse += "SERIAL: " + SERIAL + "\n\n"; 
         jsonResponse += "DOC_DT: " + DOC_DT + "\n\n"; 
         jsonResponse += "ITM_CD: " + ITM_CD + "\n\n"; 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), 
          "Error: " + e.getMessage(), 
          Toast.LENGTH_LONG).show(); 
       } 
       hidepDialog(); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(), 
         error.getMessage(), Toast.LENGTH_SHORT).show(); 
       // hide the progress dialog 
       hidepDialog(); 
      } 
     }); 
     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(jsonObjReq); 
    } 
    private void showpDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 
    private void hidepDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 
} 

Activity_main:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.theme.androidvolley.MainActivity"> 
    <Button 
     android:id="@+id/btnObjRequest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="50dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:text="Make JSON Object Request" /> 
    <TextView 
     android:id="@+id/txtResponse" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/btnObjRequest" 
     android:layout_marginTop="40px" 
     android:padding="20dp" /> 
</RelativeLayout>