-4

So this is my sample Json Text,

{
  "Date": [
    1545,
    "20 January 2014"
  ],
  "docText": "ABCD",
  "docSource": "",
  "docID": 99,
  "docDate": "",
  "Date": [
    1930,
    "1995/11",
    "past decade",
    "today",
    "Today"
  ],
  "docText": "JJJJJJJ\"",
  "Organization": [
    "x",
    "y",
    "z"
  ],
  "docSource": "",
  "docID": 98,
  "docDate": "",
  "Person": "xxxxxx"
}

Now I need a Java code to Read from this file and Display all the docText, docID Entities. I'm able to retrieve only one Entity.

This is the part of the code im using.

JSONParser jsonParser = new JSONParser();

try {

    FileReader fileReader = new FileReader(jsonFilePath);
    JSONObject jsonObject = (JSONObject) jsonParser.parse(fileReader);

    String docid = (String) jsonObject.get("docText");
    System.out.println("DocText: " +docid);

    long members = (long) jsonObject.get("docID");
    System.out.println("docID: " + members);


    JSONArray names = (JSONArray) jsonObject.get("Organization");

    Iterator i = names.iterator();

    while (i.hasNext()) {

        System.out.println("Organization: "+i.next());

I really need this working soon! Thank you

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • I think you misunderstand how Stackoverflow works. Please read the Help Center. – Sotirios Delimanolis Jun 05 '14 at 19:02
  • If you need it working soon, I suggest you start working on it. Then if you have a specific bug you are stuck on, come back here with your code and the stack trace for the error, and we can help. – forgivenson Jun 05 '14 at 19:05
  • @SotiriosDelimanolis What's the problem? – user3712390 Jun 05 '14 at 19:07
  • @forgivenson I did update my part of the code. My part of the code retrieves only one json object. I need an updated piece of code to retrieve multiple lines. – user3712390 Jun 05 '14 at 19:08
  • The problem is we are not a code writing service. Read the Help Center. – Sotirios Delimanolis Jun 05 '14 at 19:08
  • @SotiriosDelimanolis Isn't my code visible? Java program? I'm just asking what changes i need to make to my code? And yes I do know you guys are not a code writing service. – user3712390 Jun 05 '14 at 19:09
  • It's not about posting your code. You are asking us to fix your code, but haven't told us what is wrong with it. – Sotirios Delimanolis Jun 05 '14 at 19:10
  • 1
    And then you put pressure that you need it soon. That's not our problem. We'll help you if we think you deserve it. With the current state of your question, I don't think you do. – Sotirios Delimanolis Jun 05 '14 at 19:12
  • Yes, please expand on your issue. Why can you only read one json object from this file? Is there an error? – forgivenson Jun 05 '14 at 19:12
  • @SotiriosDelimanolis I think I did explain pretty clearly. I said I need the program to read both the "doctext" and "docid" entities. But I'm able to read only one entity and not the next one. Okay so you guys only fix code only if there's an error? – user3712390 Jun 05 '14 at 19:15
  • @forgivenson No there's no error. Im just wondering what more should i write in there to be able to read multiple entities or json objects? I've researched a lot but found nothing. – user3712390 Jun 05 '14 at 19:17
  • Is this homework? If not, you know there are JSON parsers out there, tried and tested? – JamesB Jun 05 '14 at 19:18
  • @JamesB Well yeah you could say that. But yes I did use JSON parsers. But they're all for reading stuff for one JSON object. My file contains multiple nested json objects. So i dont know how to read multiple lines. – user3712390 Jun 05 '14 at 19:21
  • http://stackoverflow.com/questions/7246157/how-to-parse-a-json-string-to-an-array-using-jackson – JamesB Jun 05 '14 at 19:24
  • fgb makes a very good point. You are not going to be able to read this into an object, given the duplicate field names. – JamesB Jun 05 '14 at 19:30
  • Yeah I just saw that. Thanks for the help though @JamesB – user3712390 Jun 05 '14 at 19:35

2 Answers2

1

The JSON file has duplicate keys so you probably can't read them all using a standard parser.

From http://www.ietf.org/rfc/rfc4627.txt:

An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

Instead, I'd expect each entity to be a separate object within an array.

fgb
  • 18,439
  • 2
  • 38
  • 52
0

AFAIK there is no option to fetch multiple values from a JSON object in a single get...

Hovav
  • 151
  • 2
  • 13