• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Get nth element of a JSONObject

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JSONObject that consists of 4 keys. Rather than access by key (which I don't necessarily know), I want to access by index. I found something in rpTools but I don't want to use a 3rd party library if I don't have to. Is there a native way to access JSON keys by index? TIA.
 
Ranch Hand
Posts: 127
6
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I had to learn the hard way: JSON can't be easily parsed by RegEx, and doing so at all is also only feasible if you know that the JSON has a static form. If it's dynamicly generated (wich seems the case by your "you may not know the key") chances are you have to access it by key rather than index. Or to look at it another way: If you don't know the key how you're so sure about it's always in the order you expect?
TLDR: From what I had to learn the hard way I would recommend you to use a proper JSON lib. There're several out there.
 
Bartender
Posts: 242
27
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JSON spec doesn't specify that order of keys needs to be maintained, so you can't do so consistently. What's your use case? There's probably a better way of doing it; if you need to maintain order than maybe JSON isn't the right tool for it.

From json.org:

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

 
Sam Ritter
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. It appears a linkedlist is a better approach.
 
Marshal
Posts: 3709
523
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Ritter wrote:Thanks. It appears a linkedlist is a better approach.


What would the JSON representation look like?
 
Bob Winter
Ranch Hand
Posts: 127
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Ritter wrote:Thanks. It appears a linkedlist is a better approach.


If you talk about Javas implementation java.util.LinkedList as one of the possible implementations of the java.util.List interface there're some informations about the differences between the two common implementations ArrayList and LinkedList and when one fits some usecase better than the other.
If you have an ordered data structure which depend on the order instead of keys (using keys in something like a Map is to not rely on the order) you may should reconsider if using keys is helpful in the first place.
Sure, having a data structure wich has both unique keys but also a given order can make sense in some cases, but in my eyes it should be sufficient to rely on only one of those two.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic