Android 字符串无法转换为Json数组

Android 字符串无法转换为Json数组,android,json,parsing,Android,Json,Parsing,如何将从服务器返回的数据转换为这种格式 [{"Food":"orange","Protein":"52","Water":"52","Magnesium":"12","Phosphorus":"12","Sodium":"12.5","Zinc":"45","Vit_C":"12","Fat_Sat":"12","Fat_Mono":"45","Vit_B12":"45","Calcium":"45","Calories":"4565","Fiber":"12","Cholesterole":"

如何将从服务器返回的数据转换为这种格式

[{"Food":"orange","Protein":"52","Water":"52","Magnesium":"12","Phosphorus":"12","Sodium":"12.5","Zinc":"45","Vit_C":"12","Fat_Sat":"12","Fat_Mono":"45","Vit_B12":"45","Calcium":"45","Calories":"4565","Fiber":"12","Cholesterole":"12","Potassium":"12","Sugar_Tot":"55","Iron":"45","Folic_Acid":"55","carbohydrates":"55","Vit_K":"5","Serving_Size":"\u062d\u0628\u0629"}]
对此

"[{\"Food\":\"\\u062a\\u0641\\u0627\\u062d \\u0628\\u062f\\u0648\\u0646 \\u0627\\u0644\\u0642\\u0634\\u0631\\u0629\",\"Protein\":\"0.27\",\"Water\":\"86.67\",\"Magnesium\":\"4\",\"Phosphorus\":\"11\",\"Sodium\":\"0\",\"Zinc\":\"0.05\",\"Vit_C\":\"4\",\"Fat_Sat\":\"0\",\"Fat_Mono\":\"0\",\"Vit_B12\":\"0\",\"Calcium\":\"5\",\"Calories\":\"48\",\"Fiber\":\"1\",\"Cholesterole\":\"0\",\"Potassium\":\"90\",\"Sugar_Tot\":\"10.1\",\"Iron\":\"0.07\",\"Folic_Acid\":\"0\",\"carbohydrates\":\"13\",\"Vit_K\":\"0.6\"}]"; 
因为第一种格式会导致emulator 2.2中出现异常(parssingorg.json.JSONException:java.lang.String类型的值无法转换为JSONArray)

这是我代码的一部分:
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
为“utf-8”)、8);
sb=新的StringBuilder();
字符串行=“0”;
而((line=reader.readLine())!=null){
某人附加(行);}
is.close();
结果=sb.toString();
//result=result.replace(“\”,“\”).trim();
Log.d(“”,结果);
}
捕获(例外e){
Log.e(“Log_标记”,“error”+e.toString());
}
Log.d(“”,结果);
试一试{
jArray=新的JSONArray(结果);
JSONObject json_data=null;
for(int i=0;i
返回的JSON文件不完整,它缺少结束数组的方括号

如果这个JSON总是返回,那么在末尾手动添加右方括号

// your code
result =sb.toString();
if(!result.endsWith("]")) {
  result = result + "]";
}
// continue the rest of the cod
您还可以使用此代码检查它是否是有效的JSON


我希望这能有所帮助

我假设在这行的末尾有一个
]
从服务器返回的数据是一个包含1个JsonObject的JSON数组。 您可以使用以下方法对其进行分析:

JSONArray jArray = new JSONArray(result);
String foodName = jArray.getJSONObject(0).getString("Food");

等等..

谢谢..但我认为问题在于字符串的格式。方括号被无意中省略了
JSONArray jArray = new JSONArray(result);
String foodName = jArray.getJSONObject(0).getString("Food");