[英]Parsing JSON array and object in Android
This is what the JSON looks like: JSON如下所示:
[{
"pmid": "2",
"name": " MANAGEMENT",
"result": "1",
"properties": [
{
"prop_id": "32",
"prop_name": "Bonneville",
"address": "122 Lakeshore",
"city": "Ripley",
"state": "OH",
"zip": "11454",
"lat": "41.123",
"long": "-85.5034"
}
]
}]
I am trying to parse it with the following Java code in Android: 我试图用Android中的以下Java代码解析它:
JSONObject jObj = null; JSONObject jObj = null; try { jObj = new JSONObject(jsonStr);
尝试{jObj = new JSONObject(jsonStr);
// We get weather info (This is an array)
JSONArray jArr = jObj.getJSONArray("properties");
// We use only the first value
//JSONObject JSONWeather = jArr.getJSONObject(0);
JSONObject c = jArr.getJSONObject(0);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String phone = c.getString(TAG_PHONE);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
I am not getting any results though. 我没有得到任何结果。 How can I successfully parse this JSON?
如何成功解析此JSON? I'm using Android Studio.
我正在使用Android Studio。
Also, if there were multiple pieces to the array, how could we make sure each one of them is printed out? 另外,如果阵列中有多个零件,我们如何确保将每个零件都打印出来?
Your JSON string start with JSONArray. 您的JSON字符串以JSONArray开头。
Here sample code, try it. 在这里示例代码,尝试一下。
JSONArray mJsonArray = new JSONArray(jsonStr);
JSONObject mJsonObject = mJsonArray.getJSONObject(0);
String pmid = mJsonObject.getString("pmid");
String name = mJsonObject.getString("name");
String result = mJsonObject.getString("result");
JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties");
for (int i = 0; i < mJsonArrayProperty.length(); i++) {
JSONObject mJsonObjectProperty = mJsonArrayProperty.getJSONObject(i);
String prop_id = mJsonObjectProperty.getString("prop_id");
String prop_name = mJsonObjectProperty.getString("prop_name");
String address = mJsonObjectProperty.getString("address");
String city = mJsonObjectProperty.getString("city");
String state = mJsonObjectProperty.getString("state");
String zip = mJsonObjectProperty.getString("zip");
String lat = mJsonObjectProperty.getString("lat");
String lon = mJsonObjectProperty.getString("long");
}
Here is complete example with resolution. 这是带有解析度的完整示例。
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args)
{
JSONObject jObj = null;
try {
String jsonStr = "[{\"pmid\":\"2\",\"name\":\" MANAGEMENT\",\"result\":\"1\",\"properties\":[{\"prop_id\":\"32\",\"prop_name\":\"Bonneville\",\"address\":\"122 Lakeshore\",\"city\":\"Ripley\",\"state\":\"OH\",\"zip\":\"11454\",\"lat\":\"41.123\",\"long\":\"-85.5034\"}]}]";
jsonStr = jsonStr.substring(1, jsonStr.length()-1);
System.out.println(jsonStr);
jObj = new JSONObject(jsonStr);
System.out.println("pmid="+jObj.get("pmid"));
System.out.println("name="+jObj.get("name"));
System.out.println("result="+jObj.get("result"));
JSONArray jArr = jObj.getJSONArray("properties");
JSONObject c = jArr.getJSONObject(0);
System.out.println("prop_id=="+c.get("prop_id"));
System.out.println("prop_name=="+c.get("prop_name"));
System.out.println("address=="+c.get("address"));
System.out.println("city=="+c.get("city"));
System.out.println("state=="+c.get("state"));
System.out.println("zip=="+c.get("zip"));
System.out.println("lat=="+c.get("lat"));
System.out.println("long=="+c.get("long"));
} catch (JSONException e)
{
e.printStackTrace();
}
}
}
As in posted json String jsonStr
is JSONArray of JSONObeject's instead of JOSNObject of JSONArray . 如发布的json字符串
jsonStr
是JSONObeject的JSONArray而不是JSONArray的JOSNObject 。
So convert jsonStr
String to JSONArray
: 因此将
jsonStr
String转换为JSONArray
:
JSONArray jArray = new JSONArray(jsonStr);
JSONObject c = jArray.getJSONObject(0);
// get properties JSONArray from c
JSONArray jArrProperties = c.getJSONArray("properties");
JSONObject jsonObject = jArrProperties.getJSONObject(0);
in this exammple details object contain j son data 在此详细信息对象中包含儿子数据
JSONObject details = mJSONParser.doInBackground(); //json object
Child_Registration_StaticData deta=new Child_Registration_StaticData();
try
{
deta.UniqueID = details.getString("UniqueID");
deta.Nameofchild= details.getString("Nameofchild");
deta.FatherName= details.getString("FatherName");
deta.DOB= details.getString("DOB");
child_name.setText(deta.Nameofchild);
father_name.setText(deta.FatherName);
dateof_birth.setText(deta.FatherName);
}
Your root object is a JSON array []
, not a JSON object {}
there. 您的根对象是JSON数组
[]
,而不是JSON对象{}
。 So, you need 所以,你需要
jObj = new JSONArray(jsonStr);
jObj = jObj.getJSONObject(0);
The rest of your code will now work fine treating jObj
as a JSONObject
. 现在,将
jObj
视为JSONObject
可以正常工作。 The concept here is exactly the same as what you're doing for your properties
JSON array. 这里的概念与您对
properties
JSON数组所做的完全相同。
use this 用这个
try {
JSONArray array0 = new JSONArray(Sample);
JSONObject object0 = array0.getJSONObject(0);
JSONArray array1 = object0.getJSONArray("properties");
JSONObject object1 = array1.getJSONObject(0);
String name = object1.getString("prop_name");
} catch (JSONException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.