How does the Android project read the JSON string and androidjson string from the local device?

Source: Internet
Author: User

How does the Android project read the JSON string and androidjson string from the local device?

If I have such a Demo to write, that is, I already have a JSON string at hand and want to parse it now, how should I store this JSON string in this Android project, to be read successfully. The following three solutions are available for analysis.

Assume that the JSON format is as follows:

{"Title": "Android Development", "content": "This is a test for reading JSON strings "}

(Solution 1) JSON is stored in a String

We define a String in the code, and then copy the JSON String to this String. The Code is as follows:

.


We found that this code would report an error because of quotation marks. Some values in the JSON key-value pair are not enclosed in quotation marks. Syntax error, no way, we give up this solution.


(Solution 2) JSON is stored in strings. xml in the values folder.

Android projects sometimes write strings to strings. xml in the values folder instead of Directly Writing strings in projects or layout for international purposes. This facilitates the management of the characters displayed in the project. We try to put JSON in strings. xml. The Code is as follows:
.

 

So how to read it in the code? We can read and print the JSON. The read code is as follows:

.


I print the JOSN using Log. The output is as follows. Note that the title and Android development of the key-value pairs in JSON do not have the quotation marks. The same applies to other key-value pairs. The JSON to be read in this way has a bug during parsing, so the scheme of storing JSON in strings. xml under the values folder is also passed.

.


(3) JSON is stored in an object in the assets Directory.

In the Android project, the assets Directory also compares the res directory and is also a directory for storing resources, but the resources under this directory will not be added to the apk after the application is packaged, that is, it does not occupy the application space. Now I try to create an Untitled text file in the assets Directory and copy the JSON file. Now I name this file test. json.

As follows:
.


Then how can I read it in the code? The code for reading files is as follows:

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);InputStreamReader inputStreamReader;try {inputStreamReader = new InputStreamReader(getAssets().open("test.json"), "UTF-8");BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String line;StringBuilder stringBuilder = new StringBuilder();while ((line = bufferedReader.readLine()) != null) {stringBuilder.append(line);}inputStreamReader.close();bufferedReader.close();Log.i("TAG", stringBuilder.toString());} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

I print the result again using Log, and the result is as follows:

.

The Log result of solution 3 is completely correct. It is exactly the same as the original JSON string. In this way, we can parse this JSON string. Of course, in many practical development scenarios, JSON is transmitted from the network.


My personal blog home page is http://www.chenyufengweb.com. Welcome to visit, is still improving, I hope you give suggestions.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.