2

Try make request data from JavaScript to Java using JSON format. Request body in JavaScript looks like:

{
    "id": "3",
    "name": "Chicken pasta",
    "description": "Lets make chicken pasta",
    "category": "Unassigned",
    "favorite": true,
    "prepTime": "",
    "cookTime": "",
    "ingredients": [
        {}
    ],
    "steps": [],
    "user": {
        "id": "2",
        "username": "user2"
    }
}

But on server side (in my Java controller) it is:

%7B%0A%09%22id%22%3A+%223%22%2C%0A%09%22name%22%3A+%22Chicken+pasta%22%2C%0A%09%22description%22%3A+%22Lets+make+chicken+pasta%22%2C%0A%09%22category%22%3A+%22Unassigned%22%2C%0A%09%22favorite%22%3A+true%2C%0A%09%22prepTime%22%3A+%22%22%2C%0A%09%22cookTime%22%3A+%22%22%2C%0A%09%22ingredients%22%3A+%5B%0A%09%09%7B%7D%0A%09%5D%2C%0A%09%22steps%22%3A+%5B%5D%2C%0A%09%22user%22%3A+%7B%0A%09%09%22id%22%3A+%222%22%2C%0A%09%09%22username%22%3A+%22user2%22%0A%09%7D%0A%7D=

So I'm getting JSON parsing exception. So how encode it?

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
zzheads
  • 1,368
  • 5
  • 28
  • 57
  • its encoded you have to decode it – Tanmay Sep 06 '16 at 07:22
  • use JSON.stringify() method to convert javascript object to json string – R.K.Saini Sep 06 '16 at 07:26
  • Used it already, string you see on JS side is JSON.stringify(object) – zzheads Sep 06 '16 at 07:27
  • 2
    Suggest looking into how to decode request data with mime type "application/x-www-form-urlencoded" in java - seems to be more of a java question. – traktor Sep 06 '16 at 07:45
  • 1
    The string you receive on the server is URL encoded. You need to URL decode it before you can parse it as JSON. See [this SO question](http://stackoverflow.com/questions/6138127/how-to-do-url-decoding-in-java) about URL decoding in java. – Tomas Langkaas Sep 06 '16 at 12:17

2 Answers2

1

The string you receive on the server is URL encoded. You need to URL decode it before you can parse it as JSON. See this SO question about URL decoding in java.

Community
  • 1
  • 1
Tomas Langkaas
  • 4,551
  • 2
  • 19
  • 34
0

Yes, Thomas, you right and I've found it out already. But did not used URLDecoder.decode, just wrote my own couple strings of code :)

   public static String toDecimal (String hexStr) {
        char[] hex = hexStr.toCharArray();
        char current;
        String result = "";
        for (int i=0;i<hex.length;i++) {
            if (hex[i] == '%') {
                current = (char) ((toDecimal(hex[i+1]) * 16) + toDecimal(hex[i+2]));
                i+=2;
            } else {
                if (hex[i] == '+') {
                    current = ' ';
                } else {
                    current = hex[i];
                }
            }
            result += current;
        }
        while (!(result.endsWith("}") || result.endsWith("]"))) {
            result = result.substring(0, result.length()-1);
        }
        return result;
    }

    private static int toDecimal (char hex) {
        switch (hex) {
            case '0': return 0;
            case '1': return 1;
            case '2': return 2;
            case '3': return 3;
            case '4': return 4;
            case '5': return 5;
            case '6': return 6;
            case '7': return 7;
            case '8': return 8;
            case '9': return 9;
            case 'A': return 10;
            case 'B': return 11;
            case 'C': return 12;
            case 'D': return 13;
            case 'E': return 14;
            case 'F': return 15;
        }
        return -1;
    }

Same result as mentioned earlier URLDecoder.decode method, but my function also deletes all chars from end while not found end of Json ('}' or ']'), I have no idea why, but I'm getting string from JS ends with '=' symbol.

zzheads
  • 1,368
  • 5
  • 28
  • 57