• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parse JSON using split in Java  RSS feed

 
John Joe
Ranch Hand
Posts: 359
2
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I I have this json

I want to get 123 and This is number

How to remove the "" ?
 
Knute Snortum
Sheriff
Posts: 3974
98
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the easiest way is to use JSONObject:

Then you can use the keySet() method to get all keys and the getString() method to get a key's value.
 
Knute Snortum
Sheriff
Posts: 3974
98
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer your specific question, I'd just use the substring function of String:
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are reading text from a file which includes quote marks? There must be a better way to read that file format so you get code:123 as one String minus the quote marks. Aren't there any JSON file reading tools?
What you have is what looks like the contents of a comma‑separated values file (.csv). There must be csv reading tools which can cope with that.

I think trying to parse that lot programmatically in Java® is not a good idea if you can use another tool. But if you really have to:-
Suggestion: Read the entire line, find the index of "code\":\"" and add its length. Starting thence you have an int before the next quote mark. Similarly for description, best done starting from the end of the int.
Alternative: Read the entire line. Use a regex to find code (first occurrence only) and starting from there find the first text to match an int. Find description similarly and starting from just beyond there, find all the text not matching quotes.
Alternative: Read the entire line. Pass it to a Scanner constructor and change the delimiter as you go.  Or create a regex for \{\"|\"}|\":\"|\",\" or something like that which will match the different delimiters. Use the Scanner.nextXXX methods or String#split with such a regex.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I think the easiest way is to use JSONObject: . . .
I thought there must be an easier way to do that.
 
John Joe
Ranch Hand
Posts: 359
2
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain it more? Thanks
 
John Joe
Ranch Hand
Posts: 359
2
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to POST something to postman and get the result.
 
Zachary Griggs
Ranch Hand
Posts: 82
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With JSONObject, all you have to do is create a new JSONObject from the JSON string. Then you can call the getters for the keys. For example:

 
John Joe
Ranch Hand
Posts: 359
2
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Griggs wrote:With JSONObject, all you have to do is create a new JSONObject from the JSON string. Then you can call the getters for the keys.

I tried to replace myJSONString to ,but get
JSONObject cannot applied to java.lang.String
 
Ron McLeod
Saloon Keeper
Posts: 1564
222
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your JSON is really that simple, then another approach would be to use a couple of REGEX expressions - for example:
 
Knute Snortum
Sheriff
Posts: 3974
98
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is ex.getResponseBodyAsString() only a JSON string, that is, does it have other header HTML around it?

If you have a logging system setup, I would print ex.getResponseBodyAsString() to the logger and see exactly what it is.
 
Knute Snortum
Sheriff
Posts: 3974
98
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you the wrong doc.  It should be:

https://developer.android.com/reference/org/json/JSONObject.html

And your import should be:

I don't know if this being an Android class makes a difference.  All I can say is it worked for me.
 
Dave Tolls
Ranch Hand
Posts: 2839
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will make a difference since that class is not part of the standard JDK.
Not sure what jar file would need importing, but if we're going down that route (which is fair enough) then there's any number of Json parsing libraries available.
 
Knute Snortum
Sheriff
Posts: 3974
98
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, it's not part of the standard library.  I could use the class without downloading a jar because it was a dependency in another jar I already had.

If you just want to download a jar and manually just it in your classpath, you can get the jar here:

http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm

If you use Maven, you'll need two entries: a dependency:

...and a repository:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!