解析json的方式

get_data.json
[{"id":"5","version":"5.5","name":"haha"},
{"id":"4","version":"5.4","name":"hehe"},
{"id":"3","version":"5.3","name":"hihi"}
]

/**
 *  JSONObject 解析json
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button send = findViewById(R.id.button);
        textView = findViewById(R.id.textView);

        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        if (v.getId() == R.id.button) {
            sendRequestWithOkHttp();
        }
    }

    private void sendRequestWithOkHttp()
    {
        // 开子线程
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try {
                    OkHttpClient client = new OkHttpClient();  // 创建OkHttpClient 实例
                    Request request = new Request.Builder()    // 发送请求并获取服务器返回数据(request)
                        .url("http://xx.xx.xx.xx/get_data.json")  // 获取要解析的xml
                        .build();

                    Response response = client.newCall(request).execute();// 返回数据的具体内容
                    String responseData = response.body().string();
                    // 解析json
                    parseJsonWithJSONObject(responseData);
                }
                catch (Exception e) {

                }
            }
        }).start();
    }

    private void parseJsonWithJSONObject(String jsonData)
    {
        try {
            // 将数据存到JSONArray中
            JSONArray jsonArray = new JSONArray(jsonData);
            for (int i = 0;i < jsonArray.length();i ++)
            {
                // 取出json的每个数据
                JSONObject object = jsonArray.getJSONObject(i);
                String id = object.getString("id");
                String name = object.getString("name");
                String version = object.getString("version");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2. GSON

2.1 在app/build.gradle的dependencies闭包中添加 

    //GSON
    compile 'com.google.code.gson:gson:2.7'

2.2 定义一个App类,

GSON 会将APP类映射成一个对象

            Gson gson = new Gson();
            App app = gson.fromJson(jsonData, App.class);

public class App
{
    private String id;

    private String name;

    private String version;

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getVersion()
    {
        return version;
    }

    public void setVersion(String version)
    {
        this.version = version;
    }
}
2.3 解析json数组时候需要使用到typeToken
/**
 *  GSON解析json
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button send = findViewById(R.id.button);
        textView = findViewById(R.id.textView);

        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        if (v.getId() == R.id.button) {
            sendRequestWithOkHttp();
        }
    }

    private void sendRequestWithOkHttp()
    {
        // 开子线程
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try {
                    OkHttpClient client = new OkHttpClient();  // 创建OkHttpClient 实例
                    Request request = new Request.Builder()    // 发送请求并获取服务器返回数据(request)
                        .url("http://xx.xx.xx.xx/get_data.json")  // 获取要解析的xml
                        .build();

                    Response response = client.newCall(request).execute();// 返回数据的具体内容
                    String responseData = response.body().string();
                    // 解析json
                    parseJsonWithGSON(responseData);
                }
                catch (Exception e) {

                }
            }
        }).start();
    }

    private void parseJsonWithGSON(String jsonData)
    {
        try {
            Gson gson = new Gson();
            // 使用TypeToken 将需要解析的json数组传入fromJson
            List<App> appList = gson.fromJson(jsonData, new TypeToken<List<App>>(){}.getType());
            for(App app: appList)
            {
                String id = app.getId();
                String name = app.getName();
                String version = app.getVersion();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值