android解析json


我们先在apache-tomcat-9.0.0.M17\webapps\ROOT  中创建一个后缀为json 里面放些参数  等下用来解析它  如我们这用了一个students.json:



在导入一个包  fastjson-1.2.3.jar



因为我们要解析的对象是json  所以依据内容我们要创建两实体类。

Student实体类:
package com.example.g160628_android31_parsexml;

/**
 * Created by Administrator on 2017/7/26 0026.
 */

public class Student {
    private int sid;
    private String sname;
    private String ssex;

    public Student() {
    }

    public Student(int sid, String sname, String ssex) {
        this.sid = sid;
        this.sname = sname;
        this.ssex = ssex;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex;
    }
}

然后是
BigObject实体类
package com.example.g160628_android31_parsexml;

import java.util.List;

/**
 * Created by Administrator on 2017/7/26 0026.
 */

public class BigObject {
    private int count;
    private List<Student> students;

    public BigObject() {
    }

    public BigObject(int count, List<Student> students) {
        this.count = count;
        this.students = students;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }
}

最后是 MainActivity.java

package com.example.g160628_android31_parsexml;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.AsynchronousCloseException;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void getJsonByNet(View view){
        new MyJsonTask().execute();
    }

    class  MyJsonTask extends AsyncTask{

        @Override
        protected Object doInBackground(Object[] params) {
            //获取网络json数据
            String path="http://localhost:7788/students.json";
            try {
                URL url=new URL(path);
                HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                //设置请求方式
                connection.setRequestMethod("GET");
                //设置请求超时的时间
                connection.setConnectTimeout(5000);
                //获取结果码
                int code=connection.getResponseCode();
                if(code==200){
                    //获取数据
                    InputStream is=connection.getInputStream();
                    int len=0;
                    byte[] buf=new byte[1024];
                    StringBuffer stringBuffer=new StringBuffer();
                    while ((len=is.read(buf))!=-1){
                        String s=new String(buf,0,len);
                        stringBuffer.append(s);
                    }
                    Log.i("test",stringBuffer.toString());
                    //Gson   fastJson
                    //解析Json
                    BigObject bigObject=JSON.parseObject(stringBuffer.toString(),BigObject.class);
                    int count=bigObject.getCount();
                    List<Student> students=bigObject.getStudents();
                    for (Student student : students) {
                        Log.i("test",student.getSname()+""+student.getSsex());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

如果在手机上运行还要加个权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值