-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"
    }

How to create Json Object in java using above code please give me code. i searched on stack overflow for but i have not get json object code for same json formate

3 Answers3

0

Here is sample code snippet.

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

class Data{
    List <'DATATABLE> list;
    String Mode;
}
lukkea
  • 3,606
  • 2
  • 37
  • 51
  • 1
    Welcome to Stack Overflow! While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit](http://stackoverflow.com/posts/43272560/edit) your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others. – Dev-iL Apr 07 '17 at 08:48
0

Create JSON param like it and send this param with volley request

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");

Send "paramObj" JSONObject with volley request param.

And for More detail about how use volley with JSON param can fallow this tutorials it will be helpful for you.

Garg
  • 2,731
  • 2
  • 36
  • 47
0

I have used volley to parse the above the json format. Parse your url for the below code.

Manifest file:

<?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

** * Created by root on 7/4/17. */

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>
Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19