简体   繁体   English

如何在Servlet中发布Json数据?

[英]How to Post Json Data In Servlet?

I have a json data which can be something like: 我有一个json数据,它可以是:

1st json data   
[
  {
    "id_form_delegate": "1",
    "nama_merchant": "MATAHARI BARU SERASI",
    "kota_merchant": "BALI",
    "alamat_merchant": "JL PAKUBUWONO 2D",
    "province_merchant": "BALI",
    "mid_merchant": [
      "112000902755",
      "112000902754"
    ],
    "tid_merchant": [
      "2431002547",
      "2531016215"
    ]
  }
]

or something like 或类似的东西

2nd json data
[
  {
    "id_form_delegate": "1",
    "nama_merchant": "MATAHARI BARU SERASI",
    "kota_merchant": "BALI",
    "alamat_merchant": "JL PAKUBUWONO 2D",
    "province_merchant": "BALI",
    "mid_merchant": "112000902755",
    "tid_merchant": "2431002547"
  }
]

This my servlet code 这是我的servlet代码

JSONArray arrMid = jsonObject.getJSONArray("mid_merchant");
 mid = new String[arrMid.size()];
 JSONArray arrTid = jsonObject.getJSONArray("tid_merchant");
 tid = new String[arrTid.size()];

//            
                System.out.println("nama_merchant: " + nama_merchant);
                System.out.println("kota_merchant: " + kota_merchant);
                System.out.println("alamat_merchant: " + alamat_merchant);
                System.out.println("province_merchant: " + province_merchant);
                System.out.println("mid: " + mid.length);
                System.out.println("tid: " + tid.length);

if post 1st data result success, but if I post 2nd data I've Got error 如果发布第1个数据结果成功,但是如果发布第2个数据我有错误

this my error logs 这是我的错误日志

net.sf.json.JSONException: JSONObject["mid_merchant"] is not a JSONArray. net.sf.json.JSONException:JSONObject [“ mid_merchant”]不是JSONArray。 net.sf.json.JSONException: JSONObject["tid_merchant"] is not a JSONArray. net.sf.json.JSONException:JSONObject [“ tid_merchant”]不是JSONArray。

how to get value mid_merchant.length & tid_merchant.length using JSONArray 如何使用JSONArray获得值mid_merchant.length和tid_merchant.length

Thanks 谢谢

The problem is that mid_merchant isn't an array in the second case, so you can't use JSONArray to get it's length because it has no length, it's just a String. 问题在于,在第二种情况下,mid_merchant不是数组,因此您无法使用JSONArray来获取它的长度,因为它没有长度,而只是一个字符串。 You could send an array with only one value: 您可以发送一个只有一个值的数组:

"mid_merchant": ["112000902755"],

or, if that's not what you want, you can check if it is an array (or even capture that exception) and get the String value with the getString method if it's not an array: 或者,如果这不是您想要的,则可以检查它是否为数组(甚至捕获该异常),如果不是数组,则可以使用getString方法获取String值:

String stringMid = jsonObject.getString("mid_merchant");

You can use method get(String field) and check type of result: 您可以使用方法get(String field)并检查结果类型:

Object node = jsonObject.get("mid_merchant");

String[] mid;
if (node instanceof JSONArray) {
   mid = new String[((JSONArray) node).size()];
} else {
   mid = new String[1];
}

or one-liner: 或单线:

String[] mid = new String[node instanceof JSONArray ? ((JSONArray) node).size() : 1];

Try the following code 试试下面的代码

JSONArray arrMid=json.getJSONArray("mid_merchant");
String mid=arrMid.getJSONOBJECT.getJSONObject(0).getString("mid_merchant");

JSONArray tidMid=json.getJSONArray("tid_merchant");
String mid=tidMid.getJSONOBJECT.getJSONObject(0).getString("tid_merchant");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM