Android JSON parsing simple and efficient org.json, with values such as bag

Source: Internet
Author: User

We usually use Gson to parse JSON data on Android, so it's easy to convert the data into a list or map. When you encounter a date time, you may encounter pits and need to format the datetime format.

This article describes how Org.json does not need to import any packages, regardless of how many layers of json, such as Tannangquwu.

1, Parse {"": ""} format Jsonobject

How to confirm that this is the format, we do not need to see, open http://json.cn (JSON online parsing and format validation site), the JSON data paste directly to the left, if the correct JSON data, on the right side we can see, such as:

JSON data

{"UserId": 981, "GroupID": 2, "usernickname": "xxx", "userrealname": "Wind Qingyang 1223"}

The parsed result is shown on the right, and we click the Collapse button at this point, such as:

After clicking on the graph:

As you can see, after folding, it shows object{...}, which means that at least this layer is the data in the Jsonobject format, this is good, we start to write code.

privatestaticfinal"{\"userId\":981,\"groupId\":2,\"userNickName\":\"xxx\",\"userRealName\":\"风清扬1223\"}";try {    // 新建JSONObject    new JSONObject(jsonObjectString);    // 直接可得数据    String userId = jsonObject.getString("userId");    // 打印userId    Log.d(TAG, userId);    catch (JSONException e) {    e.printStackTrace();    }

Very simple, new jsonobject, you can directly in the inside to fetch data. There's another layer underneath, for example, in the following format:

{"UserId": 981, "GroupID": 2, "usernickname": "xxx", "userrealname": "Wind Qingyang 1223", "xx": {"yy": ""}}

What if we want to get the YY value? No hurry, we still paste directly to the json.cn website:

Click the collapse button next to xx again, such as:

Can see, or an object, this is good, we will write the code:

// 新建JSONObjectnew JSONObject(jsonObjectString);// 先取得 xx的值,是个JSONObjectJSONObject yyObject = jsonObject.getJSONObject("xx");// 再 取得yy的值String yy = yyObject.getString("yy");

Very convenient, no matter how many layers below, we can in this way convenient to parse value.

2. Parsing [{"": ""},{"": "}] format of Jsonarray

Let's take a sample of the data in this format, as follows:

[{"userid": 981, "GroupID": 2, "usernickname": "xxx", "userrealname": "Wind Qing 1223"},{"userid": 123, "GroupID": 1, " Usernickname ":" yyy "," Userrealname ":" ABC "}]

or paste directly into the json.cn, such as:

This time it looks more complicated, don't worry, we'll just click on the outermost folding button:

After clicking:

As you can see, the outermost layer is an array, and we begin to write the code, first get this array,

private static final String jsonarraystring = "[{\"Userid\": 981,\"GroupID\": 2,\"Usernickname\":\"Xxx\",\"Userrealname\":\"Wind 1223\"},{\"Userid\": 123,\"GroupID\": 1,\"Usernickname\":\"yyy\",\"Userrealname\":\"Abc\"}]";//new Jsonarrayjsonarray Jsonarray = new Jsonarray (jsonarraystring);

Well, here we have got the outermost jsonarray array, which we collapsed at the outermost layer of the Json.cn website, click on the Folding button inside, as follows:

Then we see:

Is two object, can you think of jsonobject, we continue to improve the code:

// 新建JSONArraynew JSONArray(jsonArrayString);// 得到数组下标为0的JSONObjectJSONObject jsonObject0 = jsonArray.getJSONObject(0);

The focus is to get jsonobject directly through the Jsonarray getjsonobject (Index) method.

The following is a good run, all the code is as follows:

// 新建JSONArraynew JSONArray(jsonArrayString);// 得到数组下标为0的JSONObjectJSONObject jsonObject0 = jsonArray.getJSONObject(0);// 直接可取userIdString userId = jsonObject0.getString("userId");
3. Summary

For those of you who have just contacted JSON, you may be unsure of the format, and we can directly view the parsed format by pasting directly into json.cn.

There are two types: Object and Array, corresponding to Org.json, which is jsonobject and Jsonarray. The outermost layer is directly new out, which can be directly org.json the corresponding method to get.

In this way, you can easily get the value you want, no matter how many layers you have, just write more jsonobject and Jsonarray.

Finally, attach the demo: Click to download

Android JSON parsing simple and efficient org.json, with values such as bag

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.