如何在Android中解码json +集合

时间:2015-04-14 12:04:46

标签: android json

我有一个json +集合字符串如下:

//代码格式化

 { "collection" :
 {
 "version" : "1.0",
"href" : "http://example.org/friends/",

"links" : [
  {"rel" : "feed", "href" : "http://example.org/friends/rss"}
],

"items" : [
  {
    "href" : "http://example.org/friends/jdoe",
    "data" : [
      {"name" : "full-name", "value" : "J. Doe", "prompt" : "Full Name"},
      {"name" : "email", "value" : "jdoe@example.org", "prompt" : "Email"}
    ],
    "links" : [
      {"rel" : "blog", "href" : "http://examples.org/blogs/jdoe", "prompt" : "Blog"},
      {"rel" : "avatar", "href" : "http://examples.org/images/jdoe", "prompt" : "Avatar", "render" : "image"}
    ]
  },

  {
    "href" : "http://example.org/friends/msmith",
    "data" : [
      {"name" : "full-name", "value" : "M. Smith", "prompt" : "Full Name"},
      {"name" : "email", "value" : "msmith@example.org", "prompt" : "Email"}
    ],
    "links" : [
      {"rel" : "blog", "href" : "http://examples.org/blogs/msmith", "prompt" : "Blog"},
      {"rel" : "avatar", "href" : "http://examples.org/images/msmith", "prompt" : "Avatar", "render" : "image"}
    ]
  },

  {
    "href" : "http://example.org/friends/rwilliams",
    "data" : [
      {"name" : "full-name", "value" : "R. Williams", "prompt" : "Full Name"},
      {"name" : "email", "value" : "rwilliams@example.org", "prompt" : "Email"}
    ],
    "links" : [
      {"rel" : "blog", "href" : "http://examples.org/blogs/rwilliams", "prompt" : "Blog"},
      {"rel" : "avatar", "href" : "http://examples.org/images/rwilliams", "prompt" : "Avatar", "render" : "image"}
    ]
  }      
],

"queries" : [
  {"rel" : "search", "href" : "http://example.org/friends/search", "prompt" : "Search",
    "data" : [
      {"name" : "search", "value" : ""}
    ]
  }
],

"template" : {
  "data" : [
    {"name" : "full-name", "value" : "", "prompt" : "Full Name"},
    {"name" : "email", "value" : "", "prompt" : "Email"},
    {"name" : "blog", "value" : "", "prompt" : "Blog"},
    {"name" : "avatar", "value" : "", "prompt" : "Avatar"}

  ]
}
} 
}

样本linK:http://amundsen.com/media-types/collection/examples/

你能帮我解释一下如何解码这条消息。我必须阅读项目,查询我的代码以及版本等。我必须在android上执行此操作。我无法获得如何使用JSONObject来获取项目,查询我的代码。

2 个答案:

答案 0 :(得分:0)

对于数组对象,请使用JSONArray。方法arr.length()返回元素计数,arr.get<type>(position)或仅arr.get(position)以获取元素。

答案 1 :(得分:0)

你可以使用: import org.json.JSONException; import org.json.JSONObject;

以下列方式:

try {
    JSONObject myObj = new JSONObject("your json string");
} catch (JSONException e) {
                        e.printStackTrace();
                    }