perl JSON module use

Introduction:

JSON (JavaScript Object Notation) is a lightweight data interchange format.
It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition - December 1999). 
JSON is in a completely language-independent text format,
but also uses conventions similar to the C language family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.).
These properties make JSON an ideal data interchange language. Easy to read and write by humans, but also easy to parse and generate by machines.


There are two structures for JSON construction:
JSON is simply objects and arrays in javascript,
so these two structures are two structures of objects and arrays. Through these two structures, various complex structures can be represented.
1. Objects: objects in js          In object-oriented language, the key is the attribute of the object, and the value is
         the structure of the key-value pair of {key: value, key: value, ...} .
The corresponding attribute value, so it is easy to understand.
         The value method is object.key to get the attribute value. The type of this attribute value can be numbers, strings, arrays, and objects.
2. Array: Array is the content expanded by square brackets "[]" in js. The
         data structure is ["java","javascript","vb",...]. The
         value method is the same as in all languages. Obtained by index, the type of field value can be number, string, array, and object.
Through the two structures of object and array, complex data structures can be combined.


Parsing method: Parsing
JSON documents can use Perl's JOSN module, which can easily parse data
instances in various structures and combinations:

#!/usr/bin/perl
use Encode;
use JSON;
use Data::Dumper;

my $json = new JSON; #Or
convert character set my $json = JSON->new->utf8;
my $json_obj;

if(open(MYFILE, "FILE_PATH/json.html")) 
{
  print "Read json data Success.\n";
  while() 
  {
    $json_obj = $json->decode("$_"); #See
    the parsing method below 
  }
}else{
  print "Failed to read json data.\n";
}

1. Object
"title": "Urban Girl"
Code:
my $title = $json_obj->{'title'};

2. Object Combination
"images":{"small":"http:\/\/img3.douban.com\/spic\/s8968115.jpg",
          "large":"http:\/\/img3.douban.com\/lpic\/s8968115.jpg",
          "medium":"http:\/\/img3.douban.com\/mpic\/s8968115.jpg "
         }
Code:
my $imgurl = $json_obj->{'images'}->{'large'};

3. Array
"countries":["United States"],
"aka":["Girl my largest (table) ","Sisters"]}
Code:
my $aka = "";
for my $item(@{$json_obj->{'aka'}})
{                                                                                                                                               
  $aka .= $item.",";                                                                                                                            
}

4. The combination of object and array
"casts":[
         {"avatars":{"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/21417.jpg",
                     "large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/21417.jpg",
                     "medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/21417.jpg"
                    },
          "alt":"http:\/\/movie.douban.com\/celebrity\/1011562\/",
          "id":"1011562",
          "name":"刘涛"
         },
         {"avatars":{"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/34429.jpg",
                     "large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/34429.jpg",
                     "medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/34429.jpg"
                    },
         "alt":"http:\/\/movie.douban.com\/celebrity\/1044611\/",
         "id":"1044611",
         "name":"保剑锋"
         },
         {"avatars":{"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/33257.jpg",
                     "large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/33257.jpg",
                     "medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/33257.jpg"
                    },
          "alt":"http:\/\/movie.douban.com\/celebrity\/1315704\/",
          "id":"1315704",
          "name":"谢祖武"
         },
         {"avatars":null,
          "alt":null,
          "id":null,
          "name":"乔大韦"
         }
        ]
for my $item(@{$json_obj->{'casts'}})                                                                                                           
{                                                                                                                                               
  $cast .= $item->{'name'}.",";                                                                                                                 
}

 

Note: JSON parsing exceptions need to be caught.

Original text: http://blog.chinaunix.net/uid-26000296-id-3508036.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325173541&siteId=291194637