1

Kindly help me to get the subnodes list inside bom attributes

JSON file

[  
 {  
  "subConfigId":"bac",
  "totalPrice":"634.00",
  "bom":{  
     "ucid":"ucid",
     "type":"RootNode",
     "attributes":{  
        "visible":true,
        "price_status":"SUCCESS"
     },
     "subnodes":[  
        {  
           "description":"Enterprise Shock Rack",
           "ucid":"ucid"
        },
        {  
           "description":"SVC",
           "ucid":"ucid"
        }
     ]
  },
  "breakdown":{  
     "SV":550.0,
     "HW":6084.0
  },
  "currency":"USD"
 }
]

GsonNodes.java

 import java.io.FileReader;
 import java.io.IOException;
 import java.util.Iterator;
 import com.google.gson.Gson;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonParser;

  public class GsonNodes {
  public static void main(String[] args) throws IOException {   

    try{                 
     JsonElement je = new JsonParser().parse(new FileReader(
             "C:/Desktop/json.txt"));
     JsonArray ja = je.getAsJsonArray();
     Iterator itr = ja.iterator();

     while(itr.hasNext()){
         JsonElement je1 = (JsonElement) itr.next();
         Gson gson = new Gson();
         Details details = gson.fromJson(je1, Details.class);          

         System.out.println(details.getSubConfigId());
         System.out.println(details.getCurrency());
         System.out.println(details.getBreakdown());
         System.out.println(details.getTotalPrice());
         System.out.println(details.getBom().getUcid());         

     }
  } catch (Exception e) {
  e.printStackTrace();
  }
}
}

Details.java POJO

    import java.io.Serializable;
    import java.util.Map;
    public class Details implements Serializable{

        private String subConfigId;
        private String totalPrice;
        private Bom bom;     
        private String currency;
        private Map<String, String> breakdown;

        public String getSubConfigId() {
            return subConfigId;
        }
        public void setSubConfigId(String subConfigId) {
            this.subConfigId = subConfigId;
        }
        public String getTotalPrice() {
            return totalPrice;
        }
        public void setTotalPrice(String totalPrice) {
            this.totalPrice = totalPrice;
        }
        public Bom getBom() {
            return bom;
        }
        public void setBom(Bom bom) {
            this.bom = bom;
        }
        public String getCurrency() {
            return currency;
        }
        public void setCurrency(String currency) {
            this.currency = currency;
        }
        public Map<String, String> getBreakdown() {
            return breakdown;
        }
        public void setBreakdown(Map<String, String> breakdown) {
            this.breakdown = breakdown;
        }
    }

Bom.java

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;

    public class Bom implements Serializable{

        private String ucid;
        private String type;
        private Map<String, String> attributes;
        private List<Subnodes> subnodes = new ArrayList<Subnodes>();  

        public String getUcid() {
            return ucid;
        }
        public void setUcid(String ucid) {
            this.ucid = ucid;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public Map<String, String> getAttributes() {
            return attributes;
        }
        public void setAttributes(Map<String, String> attributes) {
            this.attributes = attributes;
        }   

        @Override
        public String toString(){
            return getUcid() + ", "+getType()+", "+getAttributes();
        }

    }

Subnodes.java

    import java.io.Serializable;
    import java.util.Map;

    public class Subnodes implements Serializable{

        private String description;
        private String ucid;
        private Map<String, String> attributes;

        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        public String getUcid() {
            return ucid;
        }
        public void setUcid(String ucid) {
            this.ucid = ucid;
        }
        public Map<String, String> getAttributes() {
            return attributes;
        }
        public void setAttributes(Map<String, String> attributes) {
            this.attributes = attributes;
        }   
    }

I am getting an error , when i try to get the "subnodes" I added the following code in the class

  private List<Subnodes> subnodes = new ArrayList<Subnodes>();

then i am getting the error "Expected STRING but was BEGIN_ARRAY"

kindly help me that how can i get the "subnodes" list

2 Answers 2

1

In Bom.java

Please add a getter/setter method for :

private List<Subnodes> subnodes = new ArrayList<Subnodes>();

public List<Subnodes> getSubnodes() {
    return subnodes;
}

public void setSubnodes(List<Subnodes> subnodes) {
    this.subnodes = subnodes;
}

i have tried as below .. this is working fine.

package com.brp.mvc.util;

import java.io.IOException;
import java.util.Iterator;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class GsonNodes {
    public static void main(String[] args) throws IOException {

        try {
            JsonElement je = new JsonParser().parse("[{\"subConfigId\":\"bac\",\"totalPrice\":\"634.00\",\"bom\":{\"ucid\":\"ucid\",\"type\":\"RootNode\",\"attributes\":{\"visible\":true,\"price_status\":\"SUCCESS\"},\"subnodes\":[{\"description\":\"Enterprise Shock Rack\",\"ucid\":\"ucid\"},{\"description\":\"SVC\",\"ucid\":\"ucid\"}]},\"breakdown\":{\"SV\":550.0,\"HW\":6084.0},\"currency\":\"USD\"}]");
            JsonArray ja = je.getAsJsonArray();
            Iterator itr = ja.iterator();

            while (itr.hasNext()) {
                JsonElement je1 = (JsonElement) itr.next();
                Gson gson = new Gson();
                Details details = gson.fromJson(je1, Details.class);

                System.out.println(details.getSubConfigId());
                System.out.println(details.getCurrency());
                System.out.println(details.getBreakdown());
                System.out.println(details.getTotalPrice());
                System.out.println(details.getBom().getUcid());
                System.out.println(details.getBom().getSubnodes().get(0).getDescription());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

i tried but i am getting below error java.lang.IllegalStateException: Expected STRING but was BEGIN_ARRAY at com.google.gson.internal.bind.JsonTreeReader.nextString(JsonTreeReader.java:154)com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:349)
can you please share full stackTrace ?
Thanks patel.. how to convert like this "[{\"subConfigId\":\"bac\",\"totalPrice\""
No need to convert into string .. its also working with json file as you did. it may dependency issue ... i used below dependency .. <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.1</version> </dependency>
it may file issue also .. please try with creating new file of json
|
0

i have added one method to convert json into string as below :

    public static String readFile(String filename) {
        String result = "";
        try {
            BufferedReader br = new BufferedReader(new FileReader(filename));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }
            result = sb.toString();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return result;
    }

and use this method like below :

            JsonElement je = new JsonParser().parse(readFile("C:/Desktop/json.txt"));

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.