Results 1 to 11 of 11

Thread: Json

  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default Json

    Hello everyone.

    I need to read in a file in a json format. Lets assume for simplicity purposes that there exists a String which contains the file.

    I am looking for an example which would let me parse the string, which represents json.

    I would love it if i would recognize elements in such a way so that i can get:

    Element -> Attribute 1 --> Attribute 2 { KV,KV,KV,KV}

    json.org, jsontools xstream etc web pages talk a lot about far more complicated things then what i need and i was not able to find the example i am looking for.

    I know this may not be clear enough, but do help me get clarity and point me in the right direction.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Do you have an example of a json file? Is it a disk file or do you read it from a server?
    Please give some comments/examples of what you want to "parse" out of it.

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Multiple json files will be read (all from disk). I then would need to compare their contents (to do that i will parse through the file in order to identify Key/Values, Attributes etc). I would need to be able to do something like:

    .getSpecificAttribute();


    Comparing contents of json file is the problem in need of being solved

    Example below:


    String str = "{\"attributes\":{\"PrimarySessions\":[{\"attributes\":{\"TradeAs\":[{\"attributes\":{\"FirmNumber\":[]},\"attributesAsList\":{},\"className\":\"Componen t\",\"componentName\":\"TradeAs\",\"instanceName\" :\"759.ABC759A1.XXX9\",\"type\":\"ComponentInstanc e\",\"value\":\"759.ABC759A1.XXX9\"}]},\"attributesAsList\":{},\"className\":\"Componen t\",\"componentName\":\"ILinkSession\",\"instanceN ame\":\"GLW\",\"type\":\"ComponentInstance\",\"val ue\":\"GLW\"}],\"BackupSessions\":[]},\"attributesAsList\":{},\"className\":\"Componen t\",\"componentName\":\"Gateway\",\"instanceName\" :\"GW_00001\",\"type\":\"ComponentInstance\",\"val ue\":\"GW_00001\"}";

    Norm, i reread your question .. I need to parse out of Json Keys and Value pairs
    Last edited by mac; 06-06-2010 at 09:43 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Could you give some examples of Key/Value pairs for the sample file you posted?

  5. #5
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    I would like to be able to recognize that:

    \"componentName\":\"Gateway\",
    \"instanceName\" :\"GW_00001\"
    \"type\":\"ComponentInstance\",
    \"val ue\":\"GW_00001\

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Do you have any syntax rules that define the format/layout of the json file?
    I see lots of special characters in the String you posted.

  7. #7
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    forgive my ignorance, but isn't json standard?

    general format for KV is this

    For strings

    "key":"value"

    For integers

    "key":value

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I'd never heard of json so I have no idea what would be standard for KV pairs.
    Do you have a syntax for the content of the json file?

    Are you saying the everything to the left of a : is a key and everything to right is a value?

  9. #9
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    I've mostly seen json in javascript, but I am not sure if you can or can not build a json file with java, Im curious to see if we can depict an outcome or not.
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  10. #10
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Using gson, i did:

    Java Code:
    private void runThroughJson(JsonObject jsonObject) { 
        for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) { 
     
            final String key = entry.getKey(); 
            final JsonElement value = entry.getValue(); 
     
            System.out.println(key + " - " + value); 
     
            if (value.isJsonObject()) { 
                runThroughJson(value.getAsJsonObject()); 
            } else {                 
                int ix = value.getAsString().indexOf('['); 
                int ig = value.getAsString().lastIndexOf(']'); 
     
                System.out.println(ix); 
                System.out.println(ig); 
     
                String a = value.getAsString().substring(ix, ig); 
                JsonElement jsonElement = parser.parse(a); 
                runThroughJson(jsonElement.getAsJsonObject()); 
            } 
        } 
    }

    Logically, it seems alright, however, i get an exception:

    Java Code:
    Exception in thread "main" java.lang.IllegalStateException 
        at com.google.gson.JsonArray.getAsString(JsonArray.java:133) 
        at com.cme.esg.bk.TryGson.runThroughJson(TryGson.java:46) 
        at com.cme.esg.bk.TryGson.runThroughJson(TryGson.java:44) 
        at com.cme.esg.bk.TryGson.goForIt(TryGson.java:32) 
        at com.cme.esg.bk.TryGson.main(TryGson.java:16)
    Can someone assist with this?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    JsonElement
    Where is this class defined?
    What kind of threading does it use?
    Your code appears to make some recursive calls. I don't know what a final variable does with that.

Similar Threads

  1. hashmap to json
    By prakashkadakol in forum New To Java
    Replies: 5
    Last Post: 07-30-2011, 01:32 AM
  2. JSON Lib: json-lib-1.0-jdk13.jar
    By Raghuraman K in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 10:00 AM
  3. DB Values to JSON
    By prakashkadakol in forum New To Java
    Replies: 1
    Last Post: 01-05-2010, 06:19 PM
  4. JSON Parsing
    By prakashkadakol in forum New To Java
    Replies: 1
    Last Post: 01-04-2010, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •