Json

From My Wiki
Jump to: navigation, search

JSON syntax is a subset of the JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

Tools

Formatting makes it look nicer. This is only for humans reading it because computers don't care.

use: http://jsonformat.com/

C# / Visual Studio

In Visual Studio C# use this: https://www.nuget.org/packages/Newtonsoft.Json/5.0.8

using Newtonsoft.Json;

dynamic stuff = JObject.Parse(response.Txet); 
bearer_token.Text = stuff.access_token;

Java

In java use this: https://code.google.com/p/json-simple/downloads/detail?name=json-simple-1.1.1.jar&can=2&q=

Sample code: http://git.alaning.me/root/json-example-in-java/blob/master/main.java

Another Java Library (Google Gson)

Link: https://github.com/google/gson

docs: https://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/index.html?com/google/gson/package-summary.html

How to use:

add to your java files:

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonArray;

compile and run by adding class path

-cp json-2.6.1.jar:.

Just the basics to use in your code

JsonElement jelement = new JsonParser().parse(jsonLine);
JsonObject  jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("data");
JsonArray jarray = jobject.getAsJsonArray("translations");
jobject = jarray.get(0).getAsJsonObject();
String result = jobject.get("translatedText").toString();
return result;


JSON String Examples

{ "rate_limit_context" : { "access_token" : "28491329-WORTnBwqr14b65OGJL7EPGjcuTDksl6ar4aRbB1wv" },
  "resources" : { "application" : { "/application/rate_limit_status" : { "limit" : 180,
              "remaining" : 179,
              "reset" : 1387232270
            } },
      "statuses" : { "/statuses/home_timeline" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            },
          "/statuses/mentions_timeline" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            },
          "/statuses/oembed" : { "limit" : 180,
              "remaining" : 180,
              "reset" : 1387232270
            },
          "/statuses/retweeters/ids" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            },
          "/statuses/retweets/:id" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            },
          "/statuses/retweets_of_me" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            },
          "/statuses/show/:id" : { "limit" : 180,
              "remaining" : 180,
              "reset" : 1387232270
            },
          "/statuses/user_timeline" : { "limit" : 180,
              "remaining" : 180,
              "reset" : 1387232270
            }
        },
      "users" : { "/users/contributees" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            },
          "/users/contributors" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            },
          "/users/lookup" : { "limit" : 180,
              "remaining" : 180,
              "reset" : 1387232270
            },
          "/users/profile_banner" : { "limit" : 180,
              "remaining" : 180,
              "reset" : 1387232270
            },
          "/users/search" : { "limit" : 180,
              "remaining" : 180,
              "reset" : 1387232270
            },
          "/users/show/:id" : { "limit" : 180,
              "remaining" : 180,
              "reset" : 1387232270
            },
          "/users/suggestions" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            },
          "/users/suggestions/:slug" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            },
          "/users/suggestions/:slug/members" : { "limit" : 15,
              "remaining" : 15,
              "reset" : 1387232270
            }
        }
    }
}

put that into a string called "response". Then you can do stuff like this.

dynamic stuff = JObject.Parse(response);
Console.WriteLine(stuff.resources.statuses["/statuses/user_timeline"].remaining); // 180
Console.WriteLine(stuff.resources.statuses["/statuses/user_timeline"].limit); // 180


Another example:

[ { "contributors" : null,
    "coordinates" : null,
    "created_at" : "Mon Dec 16 16:32:58 +0000 2013",
    "entities" : { "hashtags" : [  ],
        "symbols" : [  ],
        "urls" : [ { "display_url" : "api.twitter.com",
              "expanded_url" : "http://api.twitter.com",
              "indices" : [ 43,
                  65
                ],
              "url" : "http://t.co/JJ1WIaJkjK"
            },
            { "display_url" : "dev.twitter.com/discussions/24…",
              "expanded_url" : "https://dev.twitter.com/discussions/24239",
              "indices" : [ 121,
                  144
                ],
              "url" : "https://t.co/5bffaNC1i5"
            }
          ],
        "user_mentions" : [  ]
      },
    "favorite_count" : 92,
    "favorited" : false,
    "geo" : null,
    "id" : 412621381872799746,
    "id_str" : "412621381872799746",
    "in_reply_to_screen_name" : null,
    "in_reply_to_status_id" : null,
    "in_reply_to_status_id_str" : null,
    "in_reply_to_user_id" : null,
    "in_reply_to_user_id_str" : null,
    "lang" : "en",
    "place" : null,
    "possibly_sensitive" : false,
    "retweet_count" : 233,
    "retweeted" : false,
    "source" : "web",
    "text" : "Important: On Jan 14, 2014 all requests to http://t.co/JJ1WIaJkjK will require SSL & disallow plaintext connections. https://t.co/5bffaNC1i5",
    "truncated" : false,
    "user" : { "contributors_enabled" : false,
        "created_at" : "Wed May 23 06:01:13 +0000 2007",
        "default_profile" : false,
        "default_profile_image" : false,
        "description" : "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
        "entities" : { "description" : { "urls" : [  ] },
            "url" : { "urls" : [ { "display_url" : "dev.twitter.com",
                      "expanded_url" : "http://dev.twitter.com",
                      "indices" : [ 0,
                          22
                        ],
                      "url" : "http://t.co/78pYTvWfJd"
                    } ] }
          },
        "favourites_count" : 25,
        "follow_request_sent" : null,
        "followers_count" : 1982398,
        "following" : null,
        "friends_count" : 43,
        "geo_enabled" : true,
        "id" : 6253282,
        "id_str" : "6253282",
        "is_translator" : false,
        "lang" : "en",
        "listed_count" : 12259,
        "location" : "San Francisco, CA",
        "name" : "Twitter API",
        "notifications" : null,
        "profile_background_color" : "C0DEED",
        "profile_background_image_url" : "http://a0.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png",
        "profile_background_image_url_https" : "https://si0.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png",
        "profile_background_tile" : true,
        "profile_banner_url" : "https://pbs.twimg.com/profile_banners/6253282/1347394302",
        "profile_image_url" : "http://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
        "profile_image_url_https" : "https://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
        "profile_link_color" : "0084B4",
        "profile_sidebar_border_color" : "C0DEED",
        "profile_sidebar_fill_color" : "DDEEF6",
        "profile_text_color" : "333333",
        "profile_use_background_image" : true,
        "protected" : false,
        "screen_name" : "twitterapi",
        "statuses_count" : 3467,
        "time_zone" : "Pacific Time (US & Canada)",
        "url" : "http://t.co/78pYTvWfJd",
        "utc_offset" : -28800,
        "verified" : true
      }
  },
  { "contributors" : null,
    "coordinates" : null,
    "created_at" : "Fri Dec 13 15:36:02 +0000 2013",
    "entities" : { "hashtags" : [  ],
        "symbols" : [  ],
        "urls" : [  ],
        "user_mentions" : [ { "id" : 6253282,
              "id_str" : "6253282",
              "indices" : [ 41,
                  52
                ],
              "name" : "Twitter API",
              "screen_name" : "twitterapi"
            },
            { "id" : 2207775140,
              "id_str" : "2207775140",
              "indices" : [ 71,
                  84
                ],
              "name" : "Twitter Dev Brasil",
              "screen_name" : "TwitterDevBR"
            },
            { "id" : 17347849,
              "id_str" : "17347849",
              "indices" : [ 124,
                  135
                ],
              "name" : "lfcipriani",
              "screen_name" : "lfcipriani"
            }
          ]
      },
    "favorite_count" : 19,
    "favorited" : false,
    "geo" : null,
    "id" : 411519892819423232,
    "id_str" : "411519892819423232",
    "in_reply_to_screen_name" : null,
    "in_reply_to_status_id" : null,
    "in_reply_to_status_id_str" : null,
    "in_reply_to_user_id" : null,
    "in_reply_to_user_id_str" : null,
    "lang" : "en",
    "place" : null,
    "retweet_count" : 36,
    "retweeted" : false,
    "source" : "web",
    "text" : "Developers in Brazil can keep abreast of @twitterapi news by following @TwitterDevBr, our new Portuguese account managed by @lfcipriani!",
    "truncated" : false,
    "user" : { "contributors_enabled" : false,
        "created_at" : "Wed May 23 06:01:13 +0000 2007",
        "default_profile" : false,
        "default_profile_image" : false,
        "description" : "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
        "entities" : { "description" : { "urls" : [  ] },
            "url" : { "urls" : [ { "display_url" : "dev.twitter.com",
                      "expanded_url" : "http://dev.twitter.com",
                      "indices" : [ 0,
                          22
                        ],
                      "url" : "http://t.co/78pYTvWfJd"
                    } ] }
          },
        "favourites_count" : 25,
        "follow_request_sent" : null,
        "followers_count" : 1982398,
        "following" : null,
        "friends_count" : 43,
        "geo_enabled" : true,
        "id" : 6253282,
        "id_str" : "6253282",
        "is_translator" : false,
        "lang" : "en",
        "listed_count" : 12259,
        "location" : "San Francisco, CA",
        "name" : "Twitter API",
        "notifications" : null,
        "profile_background_color" : "C0DEED",
        "profile_background_image_url" : "http://a0.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png",
        "profile_background_image_url_https" : "https://si0.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png",
        "profile_background_tile" : true,
        "profile_banner_url" : "https://pbs.twimg.com/profile_banners/6253282/1347394302",
        "profile_image_url" : "http://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
        "profile_image_url_https" : "https://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
        "profile_link_color" : "0084B4",
        "profile_sidebar_border_color" : "C0DEED",
        "profile_sidebar_fill_color" : "DDEEF6",
        "profile_text_color" : "333333",
        "profile_use_background_image" : true,
        "protected" : false,
        "screen_name" : "twitterapi",
        "statuses_count" : 3467,
        "time_zone" : "Pacific Time (US & Canada)",
        "url" : "http://t.co/78pYTvWfJd",
        "utc_offset" : -28800,
        "verified" : true
      }
  }
]

Remember, square brackets mean arrays. We need to stick an array name on it. Assuming that the above is stored in a string called "response", do:

response= @"{ info: " + response + "}";

dynamic stuff = JObject.Parse(rep);

Console.WriteLine(stuff.info.Count); // 2
Console.WriteLine(stuff.info[stuff.info.Count - 1].id); //411519892819423232