Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difficulty Setting up JSON Object  RSS feed

 
Mike London
Ranch Hand
Posts: 1412
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple JSON Response saved to a file.

I read the file into a variable and then attempt to create a JSON Object so I can read the fields.

However, I'm getting a strange error that although I've tried multiple things, I cannot get past it.


Here's the error: Exception in thread "main" net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of [{,     "ISBN:1932394796": {,         "publishers": [,  .....


-----------------------

Here's the Java code:





---

Here's the actual JSON read in:


Any suggestions why I'm getting this error, and what to do about it, would be greatly appreciated.

If I check to see what the first CharAt(0) is, it's correctly: "{".

Thanks in advance,

- mike
 
Ron McLeod
Saloon Keeper
Posts: 1527
210
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which JSON library and version are you using?

Can you also post the relevant imports?
 
Mike London
Ranch Hand
Posts: 1412
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:Which JSON library and version are you using?

Can you also post the relevant imports?


Good suggestions! I also thought maybe I had an out-of-date library and indeed they were old.

Here's what my current libraries are:

com.googlecode.json-simple:json-simple:1.1.12
net.sf.json (2.4)

---

OK, so trying more examples online, this code works, but I still am not sure how to get child elements (from JSON above) like "title".



But, if I try to do something like this:



I get a "null"

Clearly, I'm missing how to identify the JSON nested nodes in this file.

Can you clarify how I would read the "identifiers" or "title"?

Thanks,

- mike
 
Mike London
Ranch Hand
Posts: 1412
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another JSON I was able to process.

One question: In the "PhoneNumbers" portion, is there a way to extract just the "type" or "number" individually, or do I need to get type and number together?

-----

JSON:


-------------------

Java Code:




------------------

OUTPUT:

John
doe
Anytown
Phone Number Data = {"number":"123-456-8888","type":"iPhone"}
Phone Number Data = {"number":"123-557-8910","type":"home"}

-----

Thanks in advance.

- mike
 
Rob Spoor
Sheriff
Posts: 21019
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike London wrote:But, if I try to do something like this:



I get a "null"

Clearly, I'm missing how to identify the JSON nested nodes in this file.

Can you clarify how I would read the "identifiers" or "title"?

jsonObject is your root JSON object, but you need to get the identifiers field of the nested field:

Note also how I use getJSONObject and getJSONArray instead of get to prevent having to cast.
 
Mike London
Ranch Hand
Posts: 1412
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, but what you posted doesn't seem to work.

In particular, I can't resolve your method "getJSONObject()" in your posted code idea.

------

My code:



I tried using both net.sf.JSON and org.json.simpleJSONArray, but nether resolves with your path:

JSONArray ids = jsonObject.getJSONObject("ISBN:1932394796").getJSONArray("identifiers");

What am I missing?

Thanks,

- mike
 
Mike London
Ranch Hand
Posts: 1412
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, part of the issue is that I was using the wrong json libraries.

There are:

"simple"
"org.sf.json"
"org.json"

Too many libraries -> Confusion.

---

Using org.json libraries, your suggested line of code now complies.

       JSONArray ids = jsonObject.getJSONObject("ISBN:1932394796").getJSONArray("identifiers");

However, given the JSON structure, I get the error that  "org.json.JSONException: JSONObject["identifiers"] is not a JSONArray."

Thanks again in advance.

- mike
 
Mike London
Ranch Hand
Posts: 1412
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out.

For my posted JSON data structure, your posted suggestion should actually have been: 

jsonObject.getJSONObject("ISBN:" + ISBN).getJSONObject("identifiers"));

I'm all set.

Thanks,

- mike
 
Rob Spoor
Sheriff
Posts: 21019
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah right, identifiers is indeed an object. I just adapted your code.

As for the library to use, I found these methods on http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/JSONObject.html, but apparently your JSONObject was the json-simple one, not the net.sf.json one. Using multiple libraries for the same purpose can become confusing this way, especially if they share class names.
 
Mike London
Ranch Hand
Posts: 1412
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Ah right, identifiers is indeed an object. I just adapted your code.

As for the library to use, I found these methods on http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/JSONObject.html, but apparently your JSONObject was the json-simple one, not the net.sf.json one. Using multiple libraries for the same purpose can become confusing this way, especially if they share class names.


Thanks Rob.

Appreciate your reply.

- mike
 
Rob Spoor
Sheriff
Posts: 21019
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!