Android creates data in JSON format

Source: Internet
Author: User

Android creates data in JSON format
Android creates JSON data 1. Creates JSON data

For Android to create JSON-format data, Android provides put (key, value) and other methods, so the code is very simple, as shown below:

JSONObject jsonObject = new JSONObject();JSONArray jsonArray = new JSONArray();JSONObject object_1 = new JSONObject();JSONObject object_2 = new JSONObject();JSONObject object_3 = new JSONObject();try {    object_1.put(id, 1);    object_1.put(ide, eclipse);    object_1.put(name, Java);    object_2.put(id, 2);    object_2.put(ide, XCode);    object_2.put(name, Swift);    object_3.put(id, 3);    object_3.put(ide, Visual Studio);    object_3.put(name, C#);    jsonArray.put(object_1);    jsonArray.put(object_2);    jsonArray.put(object_3);    jsonObject.put(languages, jsonArray);    jsonObject.put(cat, it);} catch (JSONException e) {    e.printStackTrace();}Log.i(TESTJSON, jsonObject.toString());

The output Log information is as follows:

{languages:[{id:1,ide:eclipse,name:Java},{id:2,ide:XCode,name:Swift},{id:3,ide:Visual Studio,name:C#}],cat:it}

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.