解析Android中的JSON问题

朋友您好,我在解析jSON Web服务数据时遇到问题

我从我的Web服务获得了以下数据集

[{"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"}]}] 

我正在使用此代码来解析数据,但我收到错误

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

码:

 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等待一些积极的回应和指导

谢谢

如果您要使用JSON数据,我建议您在http://json.org上查看JSON简介,直到完全理解为止。 幸运的是,JSON是一种相对简单的数据格式,并且很快就会变得舒适。

对于原始问题中的特定问题,请注意JSON数据的最外层结构是一个数组。 因此,它需要作为数组读取 – 而不是作为对象。

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

一个数组,其中一个元素是未命名的对象。 未命名的对象有12个元素,其中11个是字符串,其中一个名为“attributes”,是三个对象的数组。 “attributes”数组中的每个对象都有两个字符串元素。

因此,如果您需要“attributes”数组,首先将整个内容作为数组读取,然后将数组的第一个组件作为对象获取,然后从该对象获取“attributes”元素作为数组。 以下是这样做的一个例子。

 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")); } } } 

如果您没有使用Android提供的内置JSON API,我强烈建议您切换到Jackson ,这样可以非常轻松地使用Java读取和编写任意复杂的JSON。 以下是使用它的示例。

 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 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://sofzh.miximages.com/java/t_wifi_icon.gif // attribute name=Cashless, image=http://sofzh.miximages.com/java/t_cashless_icon.gif // attribute name=McDrive, image=http://sofzh.miximages.com/java/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 attributes; } class Attribute { public String attribute_name; public URI image_name; } 

@Richard aka cyberkiwi对这个问题的回答应该是你问题的答案。

他说:

您可能正在将STRING传递给带有前导空格的JSONObject。 尝试修剪

 JSONObject allCDs = new JSONObject(objectString.replace(/^\s+/,"")); 

编辑:我以为这是javascript。 尝试使用Java代码修改它

 JSONObject allCDs = new JSONObject(objectString.trim()); 

如果仍然无效,则显示字符串中的第一个字符:

 System.out.println((int)objectString.trim().charAt(0)); 

你应该期待123,花括号。 实际上,检查整个内容

 System.out.println((int)objectString); // or System.out.println((int)objectString.trim()); 

你也可以尝试在字符串之前切割所有内容

 JSONObject allCDs = new JSONObject(objectString.substring(objectString.indexOf('{'))); 

如果你正在使用Udacity android课程并遇到quakereport / DidUfeelIt应用程序的这个错误,那么更改URL并尝试使用其他URL来解决你的问题。 例如: – 课程期间提供的URL是“ http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6

然后我得到了同样的错误,即“解析JSON的问题”所以我尝试了不同的URL: https : //earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson

它工作.. !! 在课程期间,始终尝试从USGS网站获取最新的URL。