0

I extracted a huge String from a webpage and want to style/formatting this in Json style. The extracted String was originally a Json format but now after extracting this is just a long String. I used JsonObj for this and the formatter does curios things, he moved text from the bottom to top changed the generally the line orders etc.

http://pastebin.com/exwwc6SY JsonFile after Formatting

http://pastebin.com/WHXtE36G The extracted String

And here the code

try {
        FileWriter fw = new FileWriter("/tmp/1.txt");
        String line = ROUtils.getStringFromInputStream(urlConnection.getInputStream());
        System.out.println(line);
        String jsonObj = new JSONObject(line).toString(2);
        fw.write(jsonObj);
    } catch (IOException e) {
        e.printStackTrace();
    }

And the getStringFromInputStream() method

public static String getStringFromInputStream(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();
    String line;
    try {
        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    return sb.toString();
}

Update

I found a new issue. The JsonObj File its not equal to the original String. I compared the number of Characters (no spaces). The original String has 96311 and the JsonObj has 92636. Can anyone give me a hint what should I do?

halfer
  • 19,824
  • 17
  • 99
  • 186
Raul Vasi
  • 88
  • 1
  • 11
  • Apparently we need to sign up to filedropper to view the file, Could you use pastebin instead? – AndrewB Nov 16 '15 at 11:05
  • http://pastebin.com/VfbKsQw6 – Raul Vasi Nov 16 '15 at 11:13
  • @AndrewB no need to subscribe at all. But I submitted a review to add the links to pastebin. Didn't see anything wrong with the JSON file. The problem is the order of the elements? – guilhermerama Nov 16 '15 at 11:13
  • http://pastebin.com/cNZEy49z – Raul Vasi Nov 16 '15 at 11:13
  • The original String Starts whit { "@id":........, after the formatting he starts whit { "isLike":..... and the "@id" is now moved somewhere in the middle of the text. and that is just a exemple. – Raul Vasi Nov 16 '15 at 11:18
  • @RaulVasi edit your original post to add the links. The JSON specification at http://www.json.org/ says "an object is an unordered set of name/value pairs" so you can rely on json elements ordering. – guilhermerama Nov 16 '15 at 11:19
  • Yes, that's the main issue here. When this is parsed into a `JSONObject`, then it will internally basically be stored as a `Map`, where the properties are the *keys* of the map. The order of the map entries may be arbitrary, but from a first glance, it might be a `TreeMap`, which causes the properties to be written in alphabetical order. (EDIT: Looked it up: It seems to be a `HashMap`, so the order is arbitrary) – Marco13 Nov 16 '15 at 11:24
  • Also I would like to know what JSON api you are using @RaulVasi because you have a toString method with a parameter at `String jsonObj = new JSONObject(line).toString(2);`. What I know is the JSON libraries are free to rearrange the order of the elements as they want so whatever api you are using you **can't** rely on ordering. – guilhermerama Nov 16 '15 at 11:35
  • Found it: org.json.JSONObject - http://www.json.org/javadoc/org/json/JSONObject.html – guilhermerama Nov 16 '15 at 11:45
  • Is the JSON available publicly? Please add a link to the JSON source in your question (if its public). That would help quickly diagnose your issue and find suitable answer. – Raf Nov 16 '15 at 11:52
  • yes, the lib is json.org and the webpage is usser-pass protected, but u can use link http://pastebin.com/cNZEy49z , its the original text whitout formatting. – Raul Vasi Nov 16 '15 at 12:15
  • i downloaded the html page. Here the link: http://www.filedropper.com/httpsapiellinet-devhbz-nrwderesourcefrl3175693alljsonstylelong – Raul Vasi Nov 16 '15 at 12:22
  • So eventually, this could be a duplicate of http://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv – Marco13 Nov 16 '15 at 16:14
  • Ok guys i give it up to input in the same order as he is written but the document after formatting aren't the same! Like 4000 characters are missing, whats going worn there? – Raul Vasi Nov 17 '15 at 10:28

2 Answers2

0

You cannot and should not rely on the ordering of elements within a JSON object.

From the JSON specification at http://www.json.org/

An object is an unordered set of name/value pairs.

R_K
  • 803
  • 1
  • 7
  • 18
0

I found it out why i missed 4000 characters after converting. I forgot to close the FileWriter!

fw.close();

The close() methods calls the flush() method so that the last buffered piece of the String can written down.

Thank u guys.

Raul Vasi
  • 88
  • 1
  • 11