解析 JSON数据

37 篇文章 0 订阅
先放上一段JSON代码实例
[{"id":"5","version":"5.5","name":"Clash of Clans"},
 {"id":"6","version":7.0","name":"Boom Beach"},
 {"id":"7","version":3.5","name":"Clash Royale"}]

本文所载有两种方法对此JSON文件进行解析

一、为JSONObject

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    TextView responseText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendRequest=(Button) findViewById(R.id.send_request);
        responseText=(TextView) findViewById(R.id.response_text);
        sendRequest.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        if(v.getId()==R.id.send_request){
            Log.d("1212121","121212121212");
            sendRequestWithOkHttp();
        }
    }
    private void sendRequestWithOkHttp(){
        //开启线程来发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
               HttpsURLConnection connection=null;
                BufferedReader reader=null;
                try{
                  OkHttpClient client=new OkHttpClient();
                    Request request=new Request.Builder()
                    .url("http://10.0.2.2/get_data.json")
                            .build();
                    Response response=client.newCall(request).execute();
                    String responseData=response.body().string();
                  
                   // parseJSONWithJSONObject(responseData);这种为JSONOject方法解析
                    parseJSONWithJSONObject1(responseData);这种为GSON方法解析
                }catch(Exception e){
                    e.printStackTrace();

                }
            }
        }).start();
    }
   private void parseJSONWithJSONObject1(String jsonData){
       Gson gson=new Gson();
       List<App> appList=gson.fromJson(jsonData,new TypeToken<List<App>>(){}.getType());
       for(App app:appList){
           Log.d("MainActivity","id is "+app.getId());
           Log.d("MainActivtiy","name is "+app.getName());
           Log.d("MainActivity","version is "+app.getVersion());
       }
   }
  private void  parseJSONWithJSONObject(String jsonData){
      try{
          JSONArray jsonArray=new JSONArray(jsonData);
          for(int i=0;i<jsonArray.length();i++){
              JSONObject jsonObject=jsonArray.getJSONObject(i);
              String id=jsonObject.getString("id");
              String name=jsonObject.getString("name");
              String version=jsonObject.getString("version");
              Log.d("MainActivity","id is"+id);
              Log.d("MainActivity","name is"+name);
              Log.d("MainActivity","name is"+version);
          }
      }catch(Exception e){
          e.printStackTrace();
      }
  }

另外需注意在使用GSON方法需添加GSON库依赖和相应类的编写。本例代码如下:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
}
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;
    }
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值