aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Is This Valid JSON? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Is This Valid JSON?" Watch "Is This Valid JSON?" New topic
Author

Is This Valid JSON?

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I am having to query a SOLR server and it can return json which would be great expect I think the json is somewhat invalid. Here is a sample:



The reason I think it is invalid is because there are really 2 root elements; responseHeader and response. They are on the same level and they have no parent. So getting a JSON to Java Serializer to work with this is proving difficult. Any thoughts/opinions/suggestions?


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

It's perfectly valid to have as many properties at any level as you like.
[ July 30, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Analogous to:


Remember that a Javascript object is much more like a Java Map than it is like a Java Object. (And even Java Objects can have multiple properties, so I'm not sure where the XML-like single-root concept is coming from.)
[ July 30, 2008: Message edited by: Bear Bibeault ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Bear Bibeault:
It's perfectly valid to have as many properties at any level as you like. It does look like there's a syntax issue with quoting though.


Well, the quoting doesn't seem to be causing any problems. At least not with XStream. What I ended up having to do to get XStream to serialize it is I put a prefix and a suffix on the returned json string. So now it looks like this:



I have a SolrResponse class that contains the responseHeader and response properties which are classes themselves.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Bear Bibeault:
Analogous to:


Remember that a Javascript object is much more like a Java Map than it is like a Java Object.


Right, and if it was being dumped straight into JavaScript then sure. But I'm really only having to deal with the response in Java code. Thanks though.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Originally posted by Gregg Bolinger:
Well, the quoting doesn't seem to be causing any problems.
That was just my eyes playing trick on me.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

At a quick glance is not XStrema an XML serializer? In that case, I can see the single-root limitation. But a Java serializer should not have any issues with that perfectly valid JSON.
[ July 30, 2008: Message edited by: Bear Bibeault ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Bear Bibeault:
At a quick glance is not XStrema an XML serializer? In that case, I can see the single-root limitation. But a Java serializer should not have any issues with that perfectly valid JSON.

[ July 30, 2008: Message edited by: Bear Bibeault ]


Your are right, although XStream can also do JSON now. But I think it goes to XML and then to JSON which seems silly to me. My Java JSON lib of choice is typically FlexJSON but it only goes one way; java to json. What do you use?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

I've been using StringTree's Java->JSON. Haven't had a need yet to go the other way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is This Valid JSON?