- 
				Rangarajan
 - Posts: 18
 - Joined: 1 Jun 2007 14:51
 
JSON Example
I am looking for examples that show how to retrieve the contents of a JSON object. I have seen examples that show how to create and populate a JSON object, but I need to understand how to retrieve the different elements from an existing object.
			
			
									
									
						- 
				Thomas Linder Puls
														 - VIP Member
 - Posts: 1479
 - Joined: 28 Feb 2000 0:01
 
Re: JSON Example
A JSON object is represented as a map:
You can read about maps (and other interesting stuff) in Collection Library.
You should also notice that a json::jsonValue is a functor domain with several functor alternatives:
And with that in mind 
 here is a little example that "fetches" stuff in a JSON object:
If you need clarification then don't hesitate to ask more.
			
			
									
									Code: Select all
interface jsonObject supports mapM{string, jsonValue}
    open core, json
...You should also notice that a json::jsonValue is a functor domain with several functor alternatives:
Code: Select all
class json
    open core
 
domains
    jsonValue =
        n;
        f;
        t;
        r(real Number);
        s(string String);
        a(jsonValue* Array);
        o(jsonObject Object)
        [presenter(presenter_jsonValue)].
    % @short Represent the JSON values corresponding to the ECMA 404 standard.<br>
    % n - the JSON constant null<br>
    % f - the  JSON constant false<br>
    % t - the  JSON constant true<br>
    % r(Number) - a JSON number<br>
    % s(String) - a JSON string String<br>
    % a(Array) - a JSON array as a list<br>
    % o(Object) - a JSON object<br>
    % See also http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
    % @end
...Code: Select all
implement main
    open core
 
constants
    json : string = @'
{
    "f_int" : 12,
    "f_object" :
        {
            "nested_1" : false,
            "nested_2" : [1, 5, { "A": "a" }, 7]
        },
    "f_string" : "This is a string"
}'.
 
clauses
    run() :-
        % Parse the json string as a jsonObject
        JSON = jsonObject::fromString(json),
        stdio::writef("JSON = %p\n", JSON),
        % get the "f_object" member
        F_Object_value = JSON:get("f_object"),
        % unpack the object from the json::jsonValue
        json::o(F_Object) == F_Object_value,
        stdio::writef("F_Object = %p\n", F_Object),
        % get and unpack the "f_object" member
        F_Object_2 = JSON:get_object("f_object"),
        stdio::writef("F_Object_2 = %p\n", F_Object_2),
        % get and unpack the "nested_2" array (from F_Object)
        Nested_2 = F_Object:get_array("nested_2"),
        stdio::writef("Nested_2 = %p\n", Nested_2).
 
end implement mainRegards Thomas Linder Puls
PDC
						PDC
- 
				Rangarajan
 - Posts: 18
 - Joined: 1 Jun 2007 14:51
 
Re: JSON Example
I could get back to this example only now. Thanks again. 
Where can I get the complete documentation for reading and parsing JSON objects?
For example, how do I access the nth element of an array inside a JSON object?
Can you include a full independent example of working with JSON objects in your next update?
			
			
									
									
						Where can I get the complete documentation for reading and parsing JSON objects?
For example, how do I access the nth element of an array inside a JSON object?
Can you include a full independent example of working with JSON objects in your next update?
- 
				Thomas Linder Puls
														 - VIP Member
 - Posts: 1479
 - Joined: 28 Feb 2000 0:01
 
Re: JSON Example
JSON arrays are mapped into Prolog lists, so treatment of them follows normal list processing rules.
Looping an "N" from 0 to the length of the list and then obtaining the n'th element in the list is a very bad (in-efficient) approach to list handling. But if you insist there is an nth predicate in the list class:
However I suggest you read the Lists and Recursion tutorial first.
JSON is just a "transport" data type, and as such not a major feature of the language. It uses "normal" Visual Prolog "stuff" and should not need independent treatment. So I do not think there will be a more comprehensive example than the one already given here.
There are more relevant tutorials on the tutorials page.
			
			
									
									Looping an "N" from 0 to the length of the list and then obtaining the n'th element in the list is a very bad (in-efficient) approach to list handling. But if you insist there is an nth predicate in the list class:
Code: Select all
predicates
    nth : (positive Index, Elem* List) -> Elem Item.
    % @short Return `Item` of `List` at position `Index`. `Index` is zero based.
    % @endJSON is just a "transport" data type, and as such not a major feature of the language. It uses "normal" Visual Prolog "stuff" and should not need independent treatment. So I do not think there will be a more comprehensive example than the one already given here.
There are more relevant tutorials on the tutorials page.
Regards Thomas Linder Puls
PDC
						PDC
- 
				Rangarajan
 - Posts: 18
 - Joined: 1 Jun 2007 14:51
 
Re: JSON Example
OK. I will go through the tutorials.
Thanks.
			
			
									
									
						Thanks.
