json数据解析

Android 4.0

json数据解析——json

json数据解析
package cn.zengfansheng.jsondemo.utils;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
import android.annotation.SuppressLint;
import android.util.JsonReader;
import cn.zengfansheng.jsondemo.domain.Product;
 
/**
* json数据解析
* @author hacket
*
*/

@SuppressLint("NewApi")
public class ParseJson {
 
    /**
     * json数据解析
     * @param in
     * @return
     * @throws IOException
     */

    public static List<Product> readJsonStream(InputStream in) throws IOException {
 
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        try {
            return readProductsArray(reader);
        } finally {
          reader.close();
        }
 
      }
 
    /**
     * 将一个json文件中多个json对象封装成一个List集合
     * @param reader
     * @return
     * @throws IOException
     */

    private static List<Product> readProductsArray(JsonReader reader) throws IOException {
 
        List<Product> products = new ArrayList<Product>();
 
        reader.beginArray();
        while (reader.hasNext()) {
            products.add(readProduct(reader));
        }
        reader.endArray();
        return products;
      }
 
    /**
     * 将一个json数据对象封装成一个bean对象
     * @param reader
     * @return
     * @throws IOException
     */

    private static Product readProduct(JsonReader reader) throws IOException {
        Integer id = -1;
        Integer marketPrice = -1;
        String name = "";
        Integer activitiesPrice = -1;
        String pictureUrl = "";
 
        reader.beginObject();
        while (reader.hasNext()) {
          String jsonName = reader.nextName();
          if (jsonName.equals("id")) {
              id = reader.nextInt();
          } else if (jsonName.equals("market_price")) {
              marketPrice = reader.nextInt();
          } else if (jsonName.equals("activities_price")) {
              activitiesPrice = reader.nextInt();
          } else if (jsonName.equals("name")) {
              name = reader.nextString();
          } else {
            reader.skipValue();
          }
        }
        reader.endObject();
        return new Product(id, name, marketPrice, activitiesPrice, pictureUrl);
      }
}
JSON数据:
[
    {
        "id": 10001,
        "market_price": 659,
        "name": "Nike/耐克 春季女童跑步鞋",
        "activities_price": 399,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_1.jpg"
    },
    {
        "id": 10002,
        "market_price": 759,
        "name": "Nike/耐克童鞋 春季透气减震耐磨跑步鞋",
        "activities_price": 459,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_2.jpg"
    },
    {
        "id": 10003,
        "market_price": 619,
        "name": "Nike/耐克童鞋 春季新款 黑色女童减震高性能跑步鞋",
        "activities_price": 379,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_3.jpg"
    },
    {
        "id": 10004,
        "market_price": 719,
        "name": "Nike/耐克童鞋 春季LUNARLON减震系统跑步鞋",
        "activities_price": 419,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_4.jpg"
    },
    {
        "id": 10005,
        "market_price": 669,
        "name": "Nike/耐克童鞋 秋季LUNAR FOREVER GG深粉网布女童跑步鞋",
        "activities_price": 349,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_5.jpg"
    },
    {
        "id": 10006,
        "market_price": 759,
        "name": "BELLE/百丽2013年春季黑色小牛皮男单鞋B3355D",
        "activities_price": 599,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_6.jpg"
    },
    {
        "id": 10007,
        "market_price": 569,
        "name": "TEENMIX/天美意2013年春季啡色牛皮男单鞋36801D",
        "activities_price": 359,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_7.jpg"
    },
    {
        "id": 10008,
        "market_price": 669,
        "name": "TATA/他她2013年春季黄棕油蜡磨砂皮男单鞋D9983D",
        "activities_price": 399,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_8.jpg"
    },
    {
        "id": 10009,
        "market_price": 859,
        "name": "SENDA/森达2013年春季黑色小牛皮男单鞋2NP82D",
        "activities_price": 599,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_9.jpg"
    },
    {
        "id": 100010,
        "market_price": 999,
        "name": "TATA/他她2013年春季黑色牛皮男单鞋16-07D",
        "activities_price": 669,
        "pic_url": "http://192.168.0.102:8080/ServerDemo/images/p_10.jpg"
    }
]