如何在json响应中使用utf-8格式的数据

时间:2019-07-12 10:33:11

标签: java jquery json ajax

我试图用不同的语言保存数据。它已保存,但是当我尝试使用AJAX提取数据时,它没有提供正确的JSON数据。

以英语保存数据时,我得到正确的答复。当我用其他语言保存数据时,我会遇到此问题。我该如何解决这个问题?谁能帮我。

$.ajax({
  type: paramsObj.type, //mandatory field
  url: https://blabla.com/asset/items.json_ajax=true
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache',
    'Authorization': auth
  },
  async: true,
  data: paramsObj.data,
  dataType: 'json',
  cache: false,
  timeout: 120000,
  success: function(responseData) {
    paramsObj.success(responseData); //Invoke success callback method.
  },
  error: function(jqXHR, textStatus, errorThrown) {
    console.log("Error : " + textStatus);
    console.log(jqXHR.responseText);

  }
});

在Java控制器中处理数据后,我将数据推送到JSON对象并像这样返回

OutputStream out = null;
String jsonData = jsonObj.toString();
try {
      out = response.getOutputStream();
      response.setContentLength(jsonData.length());
      response.setHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Access-Control-Allow-Headers", "Content-Type");
      response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
      out.write(jsonData.getBytes());
      out.flush();
} catch (Exception ex) {
      ex.printStackTrace();
}

在控制器端,JSON对象是这样的:

{
  "categoryItems": {
    "Laundry": [{
      "id": 16640,
      "name": "చొక్కా",
      "shortDescription": "",
      "salesRate": 80,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16641,
      "name": "తువాలు",
      "shortDescription": "",
      "salesRate": 40,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16642,
      "name": "Curtain Door",
      "shortDescription": "",
      "salesRate": 0,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }]
  },
  "categoryIds": [1978, 1979, 1980, 1981, 1982],
  "items": [],
  "groupCategoryItems": [],
  "groupItems": [],
  "taxes": []
}

但是,JSON响应如下:

{
  "categoryItems": {,
    "Laundry": [{
      "id": 16640,
      "name": "à °šà ±Šà °•à ±Âà °•à °¾",
      "shortDescription": "",
      "salesRate": 80,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16641,
      "name": "Towel, L",
      "shortDescription": "",
      "salesRate": 40,
      "image": "fk_download",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16642,
      "name": "Curtain Door",
      "shortDescription": "",
      "salesRate": 0,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }]
  },
  "categoryIds": [1978, 1979, 1980, 1981, 1982],
  "items": [],
  "groupCategoryItems": [],
  "groupItems": []

我将控制器端代码更改为此:

OutputStream out = null;
String jsonData = jsonObj.toString();
try {
      out = response.getOutputStream();
      response.setContentLength(jsonData.length());
      response.setHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Access-Control-Allow-Headers", "Content-Type");
      response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
      response.setContentType("application/json");
      response.setCharacterEncoding("UTF-8");
      out.write(jsonData.getBytes());
      //System.out.println("---------------------");
      //System.out.println(out.toString());
      out.flush();
} catch (Exception ex) {
      ex.printStackTrace();
}

但是,我收到的JSON响应不完整

{
  "categoryItems": {
    "Dry Cleaning": [],
    "Laundry": [{
      "id": 16640,
      "name": "చొక్కా",
      "shortDescription": "",
      "salesRate": 80,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16641,
      "name": "తువాలు",
      "shortDescription": "",
      "salesRate": 40,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16642,
      "name": "Curtain Door",
      "shortDescription": "",
      "salesRate": 0,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }],
    "Pressed/Iron": [],
    "Alteration": [],
    "Repairs": []
  },
  "categoryIds": [1978, 1979, 1980, 1981, 1982],
  "items": [],
  "groupCategoryItems": [],
  "gr

1 个答案:

答案 0 :(得分:0)

您尝试过吗:

@RequestMapping(value = "/YOUR_URL_NAME", method = RequestMethod.POST, produces = "application/json; charset=utf-8");