2013-05-03 69 views
0

我在SD卡上有一个JSON对象数组。在Android应用程序中解析SD卡中的json文件

我得到的文件内容是这样的:

File yourFile = new File("/mnt/extSdCard/test.json"); 
     FileInputStream stream = new FileInputStream(yourFile); 
     String jString = null; 
     try { 
      FileChannel fc = stream.getChannel(); 
      MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 
      /* Instead of using default, pass in a decoder. */ 
      jString = Charset.defaultCharset().decode(bb).toString(); 
      } 
      finally { 
      stream.close(); 
      } 

的结构是这样的:

[{"name":"john"},{"name":"fred"},{"name":"sam"}] 

,我希望能够分析他们做出一个ListView。在JavaScript中,我可以将它们作为AJAX请求,然后执行

var people = JSON.parse(data.responseText); 

然后遍历数组。但是我在java中是一个完全新手 - 我已经找到了分别完成这些事情的示例代码,但是我不能将它们放在一起。任何帮助非常感谢。

+0

这将是更适当的解释你到目前为止尝试过什么。 – rciovati 2013-05-03 16:11:46

回答

0

试试这个

String[] from = new String[] {"name"}; 
int[] to = new int[] { R.id.name}; 
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); 

try 
{ 
    JSONArray names = new JSONArray(jsonString); 
    Log.i("MyList","Number of names " + names.length()); 
    for (int j = 0; j < names.length(); j++) 
    { 
     JSONObject jsonObject = names.getJSONObject(j); 
     HashMap<String, String> map = new HashMap<String, String>(); 
     map.put("name", jsonObject.getString("name")); 
     fillMaps.add(map); 
    } 
} 
catch (Exception e) 
{ 
    e.printStackTrace(); 
} 

SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to); 
mListView.setAdapter(adapter); 

Here mListView是您预定义的ListView。 随时分享你的疑虑,如果有的话。

+0

还没有尝试过,但我试过的其他代码永远不会通过JSONArray names = new JSONArray(jsonString); - 值[{java.lang.String不能转换为JSONArray – lucas 2013-05-03 17:15:48

+0

不,我的坏 - 这可能也会起作用。将测试并返回 – lucas 2013-05-03 18:02:34

+0

该错误是由于您的字符串。每个双引号都应该前面加一个\以标记为字符串的一部分,而不是字符串的开头或结尾。用'[{\“name \”:\“john \”},{\“name \”:\“fred \”},{\“name \”:\“sam \”}]替换您的字符串' – 2013-05-03 18:03:47

2

如果你拥有了它作为一个字符串,你应该能够将其与像这样解析到JSONObject

JSONObject jObj = null; 

// try parse the string to a JSON object 
try { 
    jObj = new JSONObject(json); 
    Log.i(TAG, "JSON Data Parsed: " + jObj.toString()); 
} catch (JSONException e) { 
    Log.e("JSON Parser", "Error parsing data " + e.toString()); 
} 

我还要把数据(在你的例子)到一个数组,所以它看起来是这样的:

{"names": [{"name": "john"},{"name": "fred"},{"name": "sam"}]} 

然后再次读取你的对象,你可以把它变成一个数组(或别的东西,我猜的)像这样的东西:

// create an empty list 
ArrayList<String> l = new ArrayList<String>(); 
// pull the array with the key 'names' 
JSONArray array = jObj.getJSONArray("names"); 
// loop through the new array 
for(int i = 0; i < array.length(); i++){ 
    // pull a value from the array based on the key 'name' 
    l.add(array.getJSONObject(i).getString("name")); 
} 

希望至少有一些帮助(或至少指出你在正确的方向)。这里也有很多资源。

编辑:

阅读上JSON格式。 []表示数组,{}表示对象,所以你有一个对象数组。这就是为什么我建议改变你的格式。如果你设置了你的格式,可以选择Mr.Me发布的答案,或者只是将字符串分成特殊字符,然后将它们放入数组中。

+0

感谢您的意见。相信我,我看了很多帖子并提供了解决方案。问题是我无法改变JSON的结构(或者我可以,但是它会破坏我试图做的目的)。我收到一个字符串,但可能是错误的方式?我已更新我的帖子以显示我如何获取文件内容。你发布的原始代码给我一个错误:Value [{java.lang。字符串不能转换为JSONObject – lucas 2013-05-03 16:29:06

+0

@lucas我更新了我的答案 – burmat 2013-05-03 16:49:29

+0

是的,我知道数组和对象之间的区别。这就是为什么我看过的例子中有那么几个没有任何用处。我没有设置我的格式,但我不创建它,所以我不想进入并手动更改它。它看起来像这样工作,但:jObj = new JSONObject()。put(“names”,jString); (完全未在更广泛的应用程序中测试,但日志确定)。如果我错过了一些东西,请告诉我,否则再次感谢您的帮助。 – lucas 2013-05-03 16:53:17

2

的问题是,上面的JSON结构代表JSONArray,而不是一个JSONObject

JSON Syntax

所以让你jstring只是这样做

JSONArray array = new JSONArray(jString); 
for(int i=0; i< array.length(); i++){ 
    JSONObject obj = array.getJSONObject(i); 
    String value = obj.getString("name"); 
} 
+0

这是正确的,应该接受 – burmat 2013-05-03 16:50:17

+0

nope。这给了我值[{java.lang.String不能转换为JSONArray(当我尝试循环jObj = new JSONObject()。put(“names”,jString); – lucas 2013-05-03 17:05:47

+0

该代码是100%正常的,请仔细检查您的数据表示形式,如果您将文件中的数据添加到问题 – 2013-05-03 17:27:14

相关问题