3

I have big String (which is not converted JSON):

{
  "MyType" : "5",
  "creationTime" : "06/03/2021 11:14:37",
  "entityCompositeKey" : "3001v4",
  ...
}

which another thousand lines.

I am only interested in value which is in MyType, I don't care about other values.

JSONObject jsonObj = new JSONObject(view);
String chiupType = jsonObj.get("MyType").toString();

but it's way too slow. What is the fastest way to get only one value from String(JSON)?

2
  • 1
    Possibly related: Is there a streaming API for JSON? Commented Jun 10, 2022 at 10:49
  • JSONObject jsonObject = new JSONObject(jsonString); int value = jsonObject.getInt("MyType"); System.out.println(value); Commented Jun 10, 2022 at 11:34

1 Answer 1

5

In the implementation, the whole JSON string is processed directly after the constructor has been called. Thus, there is no possibility to read out only a single value.

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

2 Comments

I was even thinking of taking MyType from String without even parsing because it's always comes as first value but when external app will change it - I'm done ;/ I see that String type = new JsonParser().parse(view).getAsJsonObject().get("MyType").getAsString(); is faster than JSONObject. Thanks @Sapphirex!
I would expect this argument to be accompanied with a link to the specification to proof it. Still gave an upvote since it gives a new lead to research.

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.