Cody
Cody

Reputation: 8944

Convert list of JSON String Objects

I have a web service which returns a list of JSON Objects...such as below:

[
   {
      \"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
   }
]

What is the best way to convert this String to a list of Json Objects? Is there something built in to the standard Java library? I tried using Gson, but I had no good results.

Upvotes: 0

Views: 7084

Answers (2)

Programmer Bruce
Programmer Bruce

Reputation: 66943

I tried using Gson, but I had no good results.

Solutions with either Jackson or Gson are fortunately fairly easy. The following two examples demonstrate both APIs.

JSON Input:

[
   {
      "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..."
   }
]

A Matching Java Data Structure (to deserialize):

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);
  }
}

With Jackson:

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]
  }
}

With 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]
  }
}

Note that Java data types other than String and int could be used for the Item fields. For example, both Jackson and Gson offer some built-in support to automatically bind a properly formatted JSON string to a java.util.Date.

Upvotes: 1

Maneesh
Maneesh

Reputation: 6128

There is inbuild class in android to handle the json parsing.

Regarding your example, the [ starts in your string so it means it is array so you have start like as below

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

///and so on

Upvotes: 5

Related Questions