Why do I use object1.optInt, object1.optString, and jsonobject1.optint during Json parsing?

Source: Internet
Author: User

Why do I use object1.optInt, object1.optString, and jsonobject1.optint during Json parsing?

When I was doing Json parsing today, a piece of code was not executed, so I found the cause:

1. The original code is:

A red sentence was not executed. An exception was found on the console.

 

2. Fix the bug. The correct code is

 

3. Summary

1. Our Parsing Method getString ("") is because this value does not exist in JSON, and an exception is reported, resulting in the following code not being executed.

2. I usually use the optString Method for security reasons. This method will not report exceptions.

3. If object1.optInt ("id") does not exist, 0 is returned.

Object1.optString ("mobile ")This value does not exist, and null is returned.

 

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.