Android Studio (AS) adds the ToString template that generates the JSON string __JS

Source: Internet
Author: User

There are times when you need to save the data in your interface, and it's convenient to use a JSON string.

In the app development network request this piece, has been converted to been file way so far, cannot get the original JSON string, needs to construct itself.

In practice, it is found that when the key value is constructed, the key is formatted with "", string value, such as: "\" client\ ": \" "+ client +" \ "+" + ", and the value" "format causes parsing to fail.

Perform ALT + Intert in as Java code, select ToString () display:

The default template is Concat (+).

Create a new template in the settings named Tojson.

Copy the template from Concat (+) to the new template and change it to a JSON template.


The template contents are as follows:

Public java.lang.String toString () {
#if ($members. Size () > 0)
#set ($i = 0)
Return "{" +
#foreach ($member in $members)
#if ($i = 0)
"##
#else
", ##
#end
#if ($member. Objectarray)
#if ($java _version < 5)
\ "$member. Name\": "+ ($member. accessor = null?) Null:java.util.Arrays.asList ($member. Accessor)) +
#else
\ "$member. Name\": "+ java.util.Arrays.toString ($member. Accessor) +
#end
#elseif ($member. Primitivearray && $java _version >= 5)
\ "$member. Name\": "+ java.util.Arrays.toString ($member. Accessor) +
#elseif ($member. String)
\ "$member. Name\": \ "" + $member. Accessor + "\" +
#else
\ "$member. Name\": "+ $member. Accessor +
#end
#set ($i = $i + 1)
#end
'}';
#else
Return "$classname {}";
#end
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.