0

My json string is:

{
   "recordsTotal":1331,
   "data":[
      {
         "part_number":"3DFN64G08VS8695 MS",
         "part_type":"NAND Flash",
         "id":1154,
         "manufacturers":[
            "3D-Plus"
         ]
      },
      {
         "part_number":"3DPM0168-2",
         "part_type":"System in a Package (SiP)",
         "id":452,
         "manufacturers":[
            "3D-Plus"
         ]
      },
      {
         "part_number":"3DSD1G16VS2620 SS",
         "part_type":"SDRAM",
         "id":269,
         "manufacturers":[
            "3D-Plus"
         ]
      }
   ]
}

This code lets me access the two highest level elements:

JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject(); 
System.out.println("data : " + jsonObject.get("data")); 
System.out.println("recordsTotal : " + jsonObject.get("recordsTotal"));

But what I want to do is iterate over all the objects inside "data" and create a list of part_numbers. How do I do that?

3 Answers 3

4

JsonArray is an Iterable<JsonElement>. So you can use for in loop.

JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
final JsonArray data = jsonObject.getAsJsonArray("data");
System.out.println("data : " + data);
System.out.println("recordsTotal : " + jsonObject.get("recordsTotal"));
List<String> list = new ArrayList<String>();
for (JsonElement element : data) {
    list.add(((JsonObject) element).get("part_number").getAsString());

}
Sign up to request clarification or add additional context in comments.

2 Comments

No need anymore to instantiate JsonParser. First line can be replaced by JsonObject jsonObect = JsonParser.parseString(jsonString).getAsJsonObject();
Yeah, I was hoping to avoid looping over it (seems dumb in 2025)
1

Suppose class Name for Json Model is Example.

import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Example {

    @SerializedName("recordsTotal")
    private Integer recordsTotal;
    @SerializedName("data")
    private List<Datum> data = null;

    public Integer getRecordsTotal() {
        return recordsTotal;
    }

    public void setRecordsTotal(Integer recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

    public List<Datum> getData() {
        return data;
    }

    public void setData(List<Datum> data) {
        this.data = data;
    }

}

And suppose List of Data class name is Datum :-

import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Datum {

    @SerializedName("part_number")

    private String partNumber;
    @SerializedName("part_type")

    private String partType;
    @SerializedName("id")

    private Integer id;
    @SerializedName("manufacturers")

    private List<String> manufacturers = null;

    public String getPartNumber() {
        return partNumber;
    }

    public void setPartNumber(String partNumber) {
        this.partNumber = partNumber;
    }

    public String getPartType() {
        return partType;
    }

    public void setPartType(String partType) {
        this.partType = partType;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<String> getManufacturers() {
        return manufacturers;
    }

    public void setManufacturers(List<String> manufacturers) {
        this.manufacturers = manufacturers;
    }

}

And then through Gson library we can convert json to java Model :

Example example = new Gson().fromJson(jsonString, new TypeToken<Example>() {}.getType());

Now we can get list of data though example model :-

List<Datum>  dataList = example.getData();

From dataList you can traverse and get all info.

If partNmber List we need then we can get in this way :-

 List<String> partNumberList = new ArrayList<>();
 for (Datum data : dataList) {
       partNumberList.add(data.getPartNumber());
  }

Comments

0

The given code will not guaranteed to 100% equivalent but it will help you to work.

First you have to create the class for your data objects:

class mydata {
    public String part_name;
    public String part_type;
    public int Id;
    public String manufacturers;
}

Your main method should look like

public static void main(String[] args) {

JSONObject obj = new JSONObject();
List<mydata> sList = new ArrayList<mydata>();

mydata obj1 = new mydata();
obj1.setValue("val1");
sList.add(obj1);

mydata obj2 = new mydata();
obj2.setValue("val2");
sList.add(obj2);

obj.put("list", sList);

JSONArray jArray = obj.getJSONArray("list");
for(int ii=0; ii < jArray.length(); ii++)
  System.out.println(jArray.getJSONObject(ii).getString("value"));
    }

For futher exploration you can use that link: https://gist.github.com/codebutler/2339666

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.