0
[
    {
        countryCode: "CN",
        countryCallingCode: "+86",
        codeRule: "^1\d{10}$"
    },
    {
        countryCode: "US",
        countryCallingCode: "+1",
        codeRule: "^\d{10}$"
    }
]

So I define the model like this in Kotlin

data class CountryCallingCode(
        val countryCode: String,
        val countryCallingCode: String,
        val codeRule: String? = null
)

This is what the backend document defines the response. codeRule is regex expression to verify a phone number.

I stuck in convert the String to List.

I pasted them to Android Studio, and it shows like:

String response = "[\n" +
        "    {\n" +
        "        countryCode: \"CN\",\n" +
        "        countryCallingCode: \"+86\",\n" +
        //"        codeRule: \"^1\\d{10}$\"\n" +
        "    },\n" +
        "    {\n" +
        "        countryCode: \"US\",\n" +
        "        countryCallingCode: \"+1\",\n" +
        //"        codeRule: \"^\\d{10}$\"\n" +
        "    }\n" +
        "]";

The following codes do not work.

Converting code 1:

Gson gson = new Gson()
CountryCallingCode[] countryCallingCodeList = gson.fromJson(response, CountryCallingCode[].class);

And I think the following codes are the same, correct me if I am wrong.

Converting code 2:

ArrayList<CountryCallingCode> countryCallingCodeList = (ArrayList<CountryCallingCode>)gson.fromJson(response, ArrayList.class);

Converting Code 3

Gson gson = new Gson();
        Type type = new TypeToken<List<CountryCallingCode>>() {
        }.getType();
        List<CountryCallingCode> countryCallingCodeList = gson.fromJson(response, type);

Then I use https://jsoneditoronline.org/ to reformat my json.

JsonString with codeRule online check error JsonString without codeRule online check error

I tried to remove the codeRule, and pasted to the Android Studio, it also tells me I am wrong, it shows CountryCode causes SyntaxException.

    String reponse = "[\n" +
            "    {\n" +
            "        countryCode: \"CN\",\n" +
            "        countryCallingCode: \"+86\"\n" +
            "    },\n" +
            "    {\n" +
            "        countryCode: \"US\",\n" +
            "        countryCallingCode: \"+1\"\n" +
            "    }\n" +
            "]";

Only I compress the jsonstring to oneline, I could convert the jsonstring to the array/ArrayList.

String response = "[{\"countryCode\":\"CN\",\"countryCallingCode\":\"+86\"},{\"countryCode\":\"US\",\"countryCallingCode\":\"+1\"}]";

Does anyone know

  • Q1:

How to deal with codeRule?

  • Q2:

Why couldn't I pasted the origin JsonString to the Android Studio?

Why I must compress the JsonString into one line string?

Updated:

Origin one line json string:

[{"countryCode":"CN","countryCallingCode":"+86", codeRule: "^1\d{10}$"},{"countryCode":"US","countryCallingCode":"+1", codeRule: "^\d{10}$"}] 

Pasted result json string:

String response = "[{\"countryCode\":\"CN\",\"countryCallingCode\":\"+86\", codeRule: \"^1\\d{10}$\"},{\"countryCode\":\"US\",\"countryCallingCode\":\"+1\", codeRule: \"^\\d{10}$\"}]";

Code:

Gson gson = new Gson();

/* Convertion 1 */
CountryCallingCode[] countryCallingCodeList = gson.fromJson(response, CountryCallingCode[].class);

Error:

result = {JsonSyntaxException@7237} Method threw 'com.google.gson.JsonSyntaxException' exception.
 cause = {MalformedJsonException@7241} "com.google.gson.stream.MalformedJsonException: Invalid escape sequence at line 1 column 65 path $[0].codeRule"
 detailMessage = "com.google.gson.stream.MalformedJsonException: Invalid escape sequence at line 1 column 65 path $[0].codeRule"
 stackState = null
 stackTrace = {StackTraceElement[28]@7243} 
 suppressedExceptions = {Collections$EmptyList@7244}  size = 0
 shadow$_klass_ = {Class@1694} "class com.google.gson.JsonSyntaxException"
 shadow$_monitor_ = -2082115852
4
  • A1: I believe its an issue with parsing regexes related A2: When pasting it directly, android studio added characters which should not be included in a json. It does that to keep the source formatting, which you actually don't want. The JSON format has no spaces and no \n, unless they are included in a string value Commented Jul 4, 2019 at 14:59
  • 1
    A completely valid JSON, wraps the field names in quotes as well: [ { "countryCode":"CN", "countryCallingCode":"+86", "codeRule":"^1\\d{10}$" } ] Commented Jul 4, 2019 at 15:05
  • I am not sure why you get codeRule without the quotes, but that is your issue left Commented Jul 4, 2019 at 15:11
  • @XtremeBaumer Thanks, I could run and get the list when the response is val response = "[{\"countryCode\":\"CN\",\"countryCallingCode\":\"+86\",\"codeRule\":\"^1\\\\d{10}$\"},{\"countryCode\":\"US\",\"countryCallingCode\":\"+1\",\"codeRule\":\"^\\\\d{10}$\"}]" Commented Jul 6, 2019 at 9:13

1 Answer 1

0

This is more of a comment than an answer, but I need more space to explain - if your version of Java supports it, can you try creating your response with a raw string like this:

String response = `[
    {
        countryCode: "CN",
        countryCallingCode: "+86",
        codeRule: "^1\d{10}$"
    },
    {
        countryCode: "US",
        countryCallingCode: "+1",
        codeRule: "^\d{10}$"
    }
]`

Or if possible use Kotlin, which uses """ to delimit raw strings. It will be much more readable and might help you spot mistakes

Sign up to request clarification or add additional context in comments.

2 Comments

How to create my response with a raw string like this?Do I need to type it manually?
' ' is not allowed for both Java and Kotlin, could you gave me a buildable codes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.