{
"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对象代码
答案 0 :(得分:0)
以下是示例代码段。
class DATATABLE{
String ENTITYID;
.... define all fileds here.
}
class Data{
List <'DATATABLE> list;
String Mode;
}
答案 1 :(得分:0)
创建类似它的JSON param并使用volley request
发送此paramJSONObject 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。
有关如何使用JSON param的volley如何使用this教程的更多细节,它将对您有所帮助。
答案 2 :(得分:0)
我使用了齐射来解析上面的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
** *由root创建于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>