问题解析JSON在Android中 [英] Problem in Parsing JSON in Android

查看:124
本文介绍了问题解析JSON在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好朋友我在解析JSON Web服务数据有问题。

Hello friends i am having problem in parsing jSON web services data

我有以下一组数据,我从我的Web服务得到了

I have the following set of data i got from my web services

[{"store_id":"81","store_name":"Mayo - Castlebar McDrive","store_type":"Drive-Thru",
"vouchers_available":"Vouchers available","store_limit":"10","distance":"8123.33 km",
"latitude":"53.8501090162671","longitude":"-9.29713726043701","image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/stores\/default.png",
"voucher_count":"2","is_open":"Restaurant Open","attributes":[{"attribute_name":"Wi-Fiiiii",
"image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/t_wifi_icon.gif"},{"attribute_name":"Cashless",
"image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/t_cashless_icon.gif"},
{"attribute_name":"McDrive","image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/car_icon.png"}]}]

而我会用这个code解析数据,但我得到的错误

and i m using this code to parse the DATA but i am getting the error

请我的朋友在JSON Web服务是新指导我什么我做错了。

Please friends i am new in JSON Web services guide me what am i doing wrong.

code:

JSONObject jObject = new JSONObject(data);
JSONArray array = jObject.getJSONArray("attributes");

错误:

07-19 23:43:02.437: WARN/System.err(674): org.json.JSONException: A JSONObject text must begin with '{' at character 2 of 

在等一些积极的回应和指导方针

M waiting for some positive response and guidelines

感谢

推荐答案

如果你打算使用JSON数据的工作,我建议在审查的 http://json.org ,直到完全理解。幸运的是,JSON是一个相对简单的数据格式,并成为舒适,它配备更快。

If you're going to work with JSON data, I recommend reviewing the JSON introduction at http://json.org, until it is thoroughly understood. Luckily, JSON is a relatively simple data format, and becoming comfortable with it comes quickly.

要在原来的问题的具体问题,请注意,JSON数据的最外层结构是一个数组。所以,它需要被读作为数组 - 不是作为对象

To the specific problem in the original question, note that the outer-most structure of the JSON data is an array. So, it needs to be read as an array -- not as an object.

下面是完整的JSON结构的简要说明。

Here's a brief description of the complete JSON structure.

与是一个未命名的对象一个元素的数组。该不愿透露姓名的对象有十二个元素,十一是字符串,其中一个名为属性,是三个对象的数组。在属性数组中的每个对象有两个字符串元素。

An array with one element that is an unnamed object. The unnamed object has twelve elements, eleven of which are strings, and one of which, named "attributes", is an array of three objects. Each object in the "attributes" array has two string elements.

所以,如果你想在属性阵中的全部内容先读为一个数组,然后得到数组作为对象的第一部分,然后从该对象的数组获得属性元素。下面是这样的一个例子。

So, if you want the "attributes" array, first read in the entire contents as an array, then get the first component of the array as an object, then get the "attributes" element from that object as an array. Following is an example of doing this.

import java.math.BigDecimal;
import java.net.URI;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    JSONArray outerArray = new JSONArray("[{\"store_id\":\"81\",\"store_name\":\"Mayo - Castlebar McDrive\",\"store_type\":\"Drive-Thru\",\"vouchers_available\":\"Vouchers available\",\"store_limit\":\"10\",\"distance\":\"8123.33 km\",\"latitude\":\"53.8501090162671\",\"longitude\":\"-9.29713726043701\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/stores\\/default.png\",\"voucher_count\":\"2\",\"is_open\":\"Restaurant Open\",\"attributes\":[{\"attribute_name\":\"Wi-Fiiiii\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/attributes\\/t_wifi_icon.gif\"},{\"attribute_name\":\"Cashless\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/attributes\\/t_cashless_icon.gif\"},{\"attribute_name\":\"McDrive\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/attributes\\/car_icon.png\"}]}]");
    JSONObject object = outerArray.getJSONObject(0);
    JSONArray attributes = object.getJSONArray("attributes");
    for (int i = 0, length = attributes.length(); i < length; i++)
    {
      JSONObject attribute = attributes.getJSONObject(i);
      System.out.printf("attribute name=%s, image=%s\n", attribute.getString("attribute_name"), attribute.getString("image_name"));
    }
  }
}

如果你不使用内置JSON API,它Android提供,我强烈建议切换坚持杰克逊,这使得它非常容易阅读和使用Java编写任意复杂的JSON。以下是使用它的一个例子。

If you're not stuck using the built-in JSON API that Android provides, I highly recommend switching to Jackson, which makes it very easy to read and write arbitrarily complex JSON with Java. Following is an example of using it.

import java.io.File;
import java.math.BigDecimal;
import java.net.URI;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    Store[] stores = mapper.readValue(new File("input.json"), Store[].class);
    Store store = stores[0];
    List<Attribute> attributes = store.attributes;
    for (Attribute attribute : attributes)
    {
      System.out.printf("attribute name=%s, image=%s\n", attribute.attribute_name, attribute.image_name);
    }
    // output:
    // attribute name=Wi-Fiiiii, image=http://www.mcfinder.ie/admin/images/attributes/t_wifi_icon.gif
    // attribute name=Cashless, image=http://www.mcfinder.ie/admin/images/attributes/t_cashless_icon.gif
    // attribute name=McDrive, image=http://www.mcfinder.ie/admin/images/attributes/car_icon.png
  }
}

class Store
{
  public String store_id;
  public String store_name;
  public String store_type;
  public String vouchers_available;
  public String store_limit;
  public String distance;
  public BigDecimal latitude;
  public BigDecimal longitude;
  public URI image_name;
  public int voucher_count;
  public String is_open;
  public List<Attribute> attributes;
}

class Attribute
{
  public String attribute_name;
  public URI image_name;
}

这篇关于问题解析JSON在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆