Android JSONObject – Android中的JSON解析
Android JSONObject用于android应用中的JSON解析。
在本教程中,我们将在android应用程序中讨论并实现一个" JSONObject"以解析JSON数据。
JSON代表JavaScript Object Notation。
什么是JSON?
JSON用于与服务器进行数据交换(发布和检索)。
因此,了解语法及其可用性非常重要。
JSON是XML的最佳替代方法,它对人类更具可读性。
JSON与语言无关。
由于语言的依赖性,我们可以使用任何语言(Java/C/C ++)对JSON进行编程。
来自服务器的JSON响应包含许多字段。
下面提供了一个示例JSON响应/数据。
我们将其用作参考并在我们的应用程序中实施。
{
"title":"JSONParserTutorial",
"array":[
{
"company":"Google"
},
{
"company":"Facebook"
},
{
"company":"LinkedIn"
},
{
"company" : "Microsoft"
},
{
"company": "Apple"
}
],
"nested":{
"flag": true,
"random_number":1
}
}
我们从此页面创建了一个随机的JSON数据字符串。
这对于编辑JSON数据非常方便。
JSON数据包含以下列出的4个主要组件:
- Array:JSONArray括在方括号中。
它包含一组对象 - 对象:大括号({)中包含的数据是单个JSONObject。
嵌套的JSONObjects是可能的,并且非常常用 - 密钥:每个JSONObject都有一个包含特定值的密钥字符串
- 值:每个键都有一个单一值,该值可以是字符串,双精度型,整数,布尔型等任何类型
Android JSONObject
我们将根据上面提供的静态JSON数据字符串创建JSONObject,并在ListView中显示JSONArray。
我们将应用程序名称更改为JSON数据中的标题字符串。
Android示例中的JSON解析
下图显示了用于json解析示例的android studio项目。
该项目由默认活动和布局(带有ListView)组成。
Android JSON解析代码
下面给出了" activity_main.xml"。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.theitroad.jsonparsing.MainActivity">
<ListView
android:layout_width="wrap_content"
android:id="@+id/list_view"
android:layout_height="match_parent"
</RelativeLayout>
MainActivity.java在下面给出。
package com.theitroad.jsonparsing;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String json_string = "{\n" +
" \"title\":\"JSONParserTutorial\",\n" +
" \"array\":[\n" +
" {\n" +
" \"company\":\"Google\"\n" +
" },\n" +
" {\n" +
" \"company\":\"Facebook\"\n" +
" },\n" +
" {\n" +
" \"company\":\"LinkedIn\"\n" +
" },\n" +
" {\n" +
" \"company\" : \"Microsoft\"\n" +
" },\n" +
" {\n" +
" \"company\": \"Apple\"\n" +
" }\n" +
" ],\n" +
" \"nested\":{\n" +
" \"flag\": true,\n" +
" \"random_number\":1\n" +
" }\n" +
"}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
ListView listView = (ListView) findViewById(R.id.list_view);
List<String> items = new ArrayList<>();
JSONObject root = new JSONObject(json_string);
JSONArray array= root.getJSONArray("array");
this.setTitle(root.getString("title"));
for(int i=0;i<array.length();i++)
{
JSONObject object= array.getJSONObject(i);
items.add(object.getString("company"));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
if (listView != null) {
listView.setAdapter(adapter);
}
JSONObject nested= root.getJSONObject("nested");
Log.d("TAG","flag value "+nested.getBoolean("flag"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
我们遍历了" JSONArray"对象,并获取了每个子" JSONObject"中存在的字符串,并将它们添加到了ListView中显示的ArrayList中。
应用程序名称使用来更改:
this.setTitle();
Android JSONObject示例输出
该应用程序的输出如下。
您可以在顶部的工具列中看到标题名称的更改。
Google已发布了用于JSON解析的Volley库。
我们将在以后的教程中实现。
GSON是一个Java库,可将Java对象转换为JSON,反之亦然。
我们的目标是概述android中的JSON解析,因为JSON是当今公认的用于在服务器/Web应用程序之间传输数据的标准。
当我们开发可从服务器发送和接收数据的应用程序时,Android JSON解析将非常方便。

