0

I have a json input like this

[ {
    "Company":"BEG",
    "Account":"1001",
    "Deptid":"",
    "Location":"",
    "Transaction Date":"2014-07-15",
    "Description":"Invoice",
    "Debit":0.0,
    "Credit":13.46,
    "Invoice Nbr":"1682"
    },
    {
    "Company":"BEG2",
    "Account":"1002",
    "Deptid":"23",
    "Location":"NY",
    "Transaction Date":"2014-07-15",
    "Description":"Invoice",
    "Debit":0.0,
    "Credit":13.45,
    "Invoice Nbr":"1682432"
    },
    ....
    ....
    },
    {
    "Company":"BEG99",
    "Account":"1099",
    "Deptid":"",
    "Location":"",
    "Transaction Date":"2014-07-15",
    "Description":"Invoice",
    "Debit":0.0,
    "Credit":13.46,
    "Invoice Nbr":"168243299"
    }]

I am using json simple jar to parse the json. my code is:

public String appendData(String str) throws IOException, ParseException{
        System.out.println("========Inside appendData======"+str);
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(new StringReader(str));
        double debit = (double) Double.parseDouble(jsonObject.get("Debit").toString());
}

Above code works fine, If i send single lock of data and without [ and ] bracket. Above json is a valid json according to JSONLint

Question:

1) How do I parse above json having multiple data?

4
  • 2
    you parse it into an array or collection of objects Commented Aug 15, 2014 at 19:19
  • 2
    What do you need to extract from JSON? Commented Aug 15, 2014 at 19:20
  • I need to extract all input data from json and update to my file with manipulation. i.e. my business requirement Commented Aug 15, 2014 at 19:43
  • Go to json.org and learn the JSON syntax -- it only takes 5-10 minutes. The outer [] indicates a JSON "array", yet you're apparently trying to parse it as an "object". If you understood the syntax you wouldn't make this error. Commented Aug 15, 2014 at 20:34

3 Answers 3

2

Something like below for your scenario, if you want to use json-simple:

JSONParser jsonParser = new JSONParser();
                Object obj =  jsonParser.parse(new StringReader(str));
            if (obj instanceof JSONObject) {
                JSONObject jo = (JSONObject) obj;
                 System.out.println(jo.get("Debit").toString());
            } else {
                JSONArray ja = (JSONArray) obj;
                for(int i=0;i<ja.size();i++){
                    JSONObject   jsonObject = (JSONObject)ja.get(i);
                    System.out.println(jsonObject.get("Debit").toString());
                }
            }
Sign up to request clarification or add additional context in comments.

Comments

1

The GSON library is very easy for this kind of tasks

Comments

0

https://code.google.com/p/json-simple/wiki/DecodingExamples

You'll see that to process your array instead of JSONObject you need to us JSONArray. The link has a full example.

I typically use Jackson for my JSON parsing, it is pretty good.

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.