转换JSON字符串对象列表

时间:2011-12-07 05:12:54

标签: java android json

我有一个Web服务,它返回一个JSON对象列表......如下所示:

[
   {
      \"ItemId\":22,
      \"ItemName\":\"ASA 5505\",
      \"AssetNmbr\":\"\",
      \"EmployeeId\":\"\",
      \"Type\":\"\",
      \"Category\":\"\",
      \"PurchaseDt\":\"\",
      \"PurchaseBy\":null,
      \"IssueDt\":\"\",
      \"IssueBy\":null,
      \"Price\":\"300.00\",
      \"SerialNmbr\":\"JMX1538408E\",
      \"MacAddress\":null,
      \"LocationNm\":\"Stock\",
      \"Notes\":null
   }
]

将此String转换为Json对象列表的最佳方法是什么?标准Java库中是否有内置的东西?我尝试使用Gson,但效果不佳。

2 个答案:

答案 0 :(得分:5)

android中有inbuild类来处理json解析。

关于你的例子,[从你的字符串开始,这意味着它是数组,所以你开始如下所示

JSONArray start_object=new JSONArray(inputString);
JSONObject jObject = start_object.getJSONObject(0);
String itemid= jObject.getString("Itemid");

///等等

答案 1 :(得分:1)

  

我尝试过使用Gson,但效果不佳。

幸运的是,使用Jackson或Gson的解决方案相当容易。以下两个示例演示了两种API。

JSON输入:

[
   {
      "ItemId":22,
      "ItemName":"ASA 5505",
      "AssetNmbr":"",
      "EmployeeId":"",
      "Type":"",
      "Category":"",
      "PurchaseDt":"",
      "PurchaseBy":null,
      "IssueDt":"",
      "IssueBy":null,
      "Price":"300.00",
      "SerialNmbr":"JMX1538408E",
      "MacAddress":null,
      "LocationNm":"Stock",
      "Notes":null
   },
   {
      "ItemId":11,
      "ItemName":"QRQ 1234",
      "AssetNmbr":"A1",
      "EmployeeId":"42",
      "Type":"BIG",
      "Category":"ORANGE",
      "PurchaseDt":"yesterday",
      "PurchaseBy":null,
      "IssueDt":"",
      "IssueBy":null,
      "Price":"1.00",
      "SerialNmbr":"CJEUI323EK22",
      "MacAddress":null,
      "LocationNm":"Purple",
      "Notes":"the quick brown fox..."
   }
]

匹配的Java数据结构(反序列化):

class Item
{
  int ItemId;
  String ItemName;
  String AssetNmbr;
  String EmployeeId;
  String Type;
  String Category;
  String PurchaseDt;
  String PurchaseBy;
  String IssueDt;
  String IssueBy;
  String Price;
  String SerialNmbr;
  String MacAddress;
  String LocationNm;
  String Notes;

  @Override
  public String toString()
  {
    return String.format("Item: ItemId=%d, ItemName=%s, Price=%s, SerialNmbr=%s", ItemId, ItemName, Price, SerialNmbr);
  }
}

杰克逊:

import java.io.File;
import java.util.List;

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);

    List<Item> items = mapper.readValue(new File("input.json"), new TypeReference<List<Item>>(){});
    System.out.println(items);
    // output:
    // [Item: ItemId=22, ItemName=ASA 5505, Price=300.00, SerialNmbr=JMX1538408E, 
    //  Item: ItemId=11, ItemName=QRQ 1234, Price=1.00, SerialNmbr=CJEUI323EK22]
  }
}

使用Gson:

import java.io.FileReader;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonFoo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();

    List<Item> items = gson.fromJson(new FileReader("input.json"), new TypeToken<List<Item>>(){}.getType());
    System.out.println(items);
    // output:
    // [Item: ItemId=22, ItemName=ASA 5505, Price=300.00, SerialNmbr=JMX1538408E, 
    //  Item: ItemId=11, ItemName=QRQ 1234, Price=1.00, SerialNmbr=CJEUI323EK22]
  }
}

请注意,String和int以外的Java数据类型可用于Item字段。例如,Jackson和Gson都提供了一些内置支持,可以将格式正确的JSON字符串自动绑定到java.util.Date