can some one please get me the json parsing for an json array
line = "contexts": [
{
"uuid": "6686feaa-a254-42ec-a662-c36f70f7a586",
"name": "School",
},
{
"uuid": "bd8e6c44-d461-4bbe-8946-a3717dc7fa7f",
"name": "Teaching",
}]
I need uuid and name into to string arrays. I tried
String[] x = new String[10];
String[] y = new String[10];
JSONArray jArray = new JSONArray(line);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
x = json_data.getString("name");
y = json_data.getString("uuid");
}
I get type mismatch error when I run this. The type of line is string which I return from server.
use i to add elements to Array's:
String[] x = new String[10];
String[] y = new String[10];
JSONObject json=new JSONObject(line);
JSONArray jArray =json.getJSONArray("contexts");
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
x[i] = json_data.getString("name");
y[i] = json_data.getString("uuid");
}
you can use http://jsonviewer.stack.hu/ for checking json is valid or not. and use ArrayList instead of Array for getting items dynamically from web service
Its neither JSONObject, nor JSONArray, return string from server like below:
line = {"contexts": [
{
"uuid": "6686feaa-a254-42ec-a662-c36f70f7a586",
"name": "School",
},
{
"uuid": "bd8e6c44-d461-4bbe-8946-a3717dc7fa7f",
"name": "Teaching",
}]}
and then parse like below code:
JSONObject json=new JSONObject(line);
JSONArray jArray =json.getJSONArray("contexts");
Related
I don't know how to parse a JSON if start with jsonArray instead jsonObject.
There's the JSON code.
[
{
"id": 1,
"title":
{
"rendered": "Apple apologises and fixes security flaw"
}
},
{
"id": 2,
"title":
{
"rendered": "Trophy hunting removes good genes and raises extinction risk"
}
}
...
]
I don't know how to get the JSONArray length. such as:
for (int i = 0; i < JSONArray.length(); i++)
{
JSONObject JSONObject1 = JSONArray.getJSONObject(i);
int id = JSONObject1.getInt("id");
string title = JSONObject1.getString("rendered");
}
Any help will be greatly appreciated!
Assuming that your json value is in this string variable : json
String json = "your_json_data";
JSONArray jsonarray = new JSONArray(json);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObject = jsonarray.getJSONObject(i);
int id = jsonObject.getInt("id");
JSONObject titleObject = jsonObject.getJSONObject("title");
String rendered = titleObject.getString("rendered");
}
Try below code
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObject= jsonarray.getJSONObject(i);
int id = jsonObject.getInt("id");
JSONObject jsonInner= jsonObject.getJSONObject("title");
string title = jsonInner.getString("rendered");
}
This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
this is array which i have
[ {
"comment": "Sir, 2nd paper qualifying hai ya Uski bhi merit banegi?",
"user": "Sameer",
"image": "https://jdcivils.org/images/co.png",
"date": "04/Sep/2016",
"reply": [
{
"comment": "Abhi qualifying nhi hai,iske marks merit list me judte hain",
"user": "Admin",
"image": "https://jdcivils.org/images/co.png",
"date": "04/Sep/2016"
}
]
},
{
"comment": "Cgpsc me hight kis kis post ke liye jaruri hoti hai..\r\nplss btayega koi??",
"user": "Vinod kumar Yadav",
"image": "https://jdcivils.org/images/co.png",
"date": "29/Aug/2016",
"reply": ""
},
{
"comment": "Sir Cgpsc Me gen. category walo Ko Psc k attempts ki koi limit Hoti Hai kya??",
"user": "Vinod kumar Yadav",
"image": "https://jdcivils.org/images/co.png",
"date": "28/Aug/2016",
"reply": [
{
"comment": "nahi",
"user": "Admin",
"image": "https://jdcivils.org/images/co.png",
"date": "28/Aug/2016"
}
]
}]
Try in this way
JSONArray jsonarray = new JSONArray(yourresponse))
for(int i=0;i<jsonarray.length;i++){
JSONObject jsonobject = jsonarray.getJsonObject(i);
String comment = jsonobject.getString("comment");
String user = jsonobject.getString("user");
String image = jsonobject.getString("image");
String date = jsonobject.getString("date");
JSONArray jsonarray1 = jsonobject.getJSONArray("reply");
for(int j=0; j<jsonarray1.length;j++){
String comment1 = jsonobject.getString("comment");
String user1 = jsonobject.getString("user");
String image1 = jsonobject.getString("image");
String date1 = jsonobject.getString("date");
}
}
I am assuming you are getting string in respose so convert it to jsonArray
JSONArray SamplejsonArray = new JSONArray(String);
for(int i = 0;i<SamplejsonArray .length();i++){
JSONObject SamplejsonObject = SamplejsonArray .getJsonObject(i);
String comment = SamplejsonObject .getString("comment");
String user = SamplejsonObject .getString("user");
String image= SamplejsonObject .getString("image");
String date= SamplejsonObject .getString("date");
JSONArray replyjsonArray = SamplejsonObject.getJSONArray(reply);
for(int j = 0;i<replyjsonArray .length();j++){
JSONObject replyjsonObject = replyjsonArray.getJsonObject(j);
String comment = replyjsonObject .getString("comment");
String user = replyjsonObject .getString("user");
String image= replyjsonObject .getString("image");
String date= replyjsonObject .getString("date");
}
}
Use json to pojo converter for create model class from complex json array
Refer link : http://www.jsonschema2pojo.org/
Use this library, all you need is create model with same key as string that you have. Gson parser
<Your model> value = new Gson().fromJson(new String(<Ur string>.getBytes("ISO-8859-1"),
"UTF-8"), <Your model>.class);
try this way
JSONArray jsonArray = new JSONArray(sampleString);
for(int i = 0;i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJsonObject(i)
String comment = jsonObject.getString("comment");
String user = jsonObject.getString("user");
JSONArray replyArray = jsonObject.getJSONArray("reply");
for(int j = 0;j<replyArray.length();j++){
JSONObject innerObject = replyArray.getJSONObject(j);
String comment = innerObject.getString("comment"); ...
}
}
try {
//Create array object from String object that contains your json array
JSONArray jArray = new JSONArray(jsonString);
//Get JSONObject from array
jArray.get(0);
} catch (JSONException e) {
e.printStackTrace();
}
I have this json object as an output of a web service , I want to print each part_Name node on a separate android text view how to do that?
{
"0": [],
"1": {
"Part_ID": "1",
"Part_NAME": "part_name_one "
},
"2": {
"Part_ID": "2",
"Part_NAME": " part_name_two "
},
"3": {
"Part_ID": "3",
"Part_NAME": "part_name_three"
},
I tried this code but I don't get an output in my textview
jobj = jsonparser.makeHttpRequest("http://192.168.1.7:89/My_website/My_Webservice.php");
try {
JSONObject jsonRootObject = new JSONObject(jobj.toString());
JSONArray jsonArray = jsonRootObject.optJSONArray("Services_Parts");
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("Part_NAME").toString();
data += "Node"+i+ "+ name +" +name+ " \n ";
tv2.setText(name);
}
if possible then try to change the structure of your json to something like that
[
{
"Part_ID": "1",
"Part_NAME": "part_name_one "
},
{
"Part_ID": "2",
"Part_NAME": " part_name_two "
},
{
"Part_ID": "3",
"Part_NAME": "part_name_three"
}
]
put each json object into an jsonarray .. as shown above
then parse each jsonobject and create the textview dynamically acc. to no. of jsonobject inside jsonarray
using the above ex. there are 3 jsonobject inside jsonarray
hence create 3 textview dynamically at the time of json parsing
JSONArray jsonArray = <your json array>;
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("Part_NAME").toString();
addTextView(name);
}
// add text to dynamically created textview code..
public void addTextView(String text)
{
final TextView textView = new TextView(this);
textView.setText(text);
myLinearLayout.addView(textView);
}
Can any body tell me how I parse this type of json in android?
[
[
{
"condition_id":"1",
"condition_name":"Type 1 Diebetics"
}
],
[
{
"condition_id":"2",
"condition_name":"Type 2 dypatise"
}
]
]
Thanks
Solved
Thank you very much nayoso
Its worked for me.
String jsonString = "[
[
{
"condition_id":"1",
"condition_name":"Type 1 Diebetics"
}
],
[
{
"condition_id":"2",
"condition_name":"Type 2 dypatise"
}
]
]";
JSONArray jsonArray = new JSONArray(jsonString);
for(int i=0;i<jsonArray.length(),i++) {
JSONArray childJsonArray = jsonArray.getJSONArray(i);
JSONObject contentJsonObject = childJsonArray.getJSONObject(0);
String conditionID = contentJsonObject.getString('condition_id');
String conditionName = contentJsonObject.getString('condition_name');
Log.i("TAG","Index "+i+" condition_id "+conditionID+" condition_name "+conditionName);
}
You can do this easily with the org.json library. The whole thing is a JSONArray; at position 0 (use .get(0)) you have another JSONArray; at position 0 of that, you have a JSONObject, which maps keys to values (use .getString()).
Thanks chiastic-security also for making me understand.
You can use built in JSONArray and JSONObject classes
String jsonString = "[
[
{
"condition_id":"1",
"condition_name":"Type 1 Diebetics"
}
],
[
{
"condition_id":"2",
"condition_name":"Type 2 dypatise"
}
]
]";
JSONArray jsonArray = new JSONArray(jsonString);
for(int i=0;i<jsonArray.length(),i++) {
JSONArray childJsonArray = jsonArray.getJSONArray(i);
JSONObject contentJsonObject = childJsonArray.getJSONObject(0);
String conditionID = contentJsonObject.getString('condition_id');
String conditionName = contentJsonObject.getString('condition_name');
Log.i("TAG","Index "+i+" condition_id "+conditionID+" condition_name "+conditionName);
}
You can do this easily with the org.json library. The whole thing is a JSONArray; at position 0 (use .get(0)) you have another JSONArray; at position 0 of that, you have a JSONObject, which maps keys to values (use .getString()).
JSONArray jsonarray=new JSONArray(your_data);
for(int i=0;i<jsonarray.length();i++)
{
JSONArray array=jsonarray.getJSONArray(i);
for(int j=0;j<array.length();j++)
{
JSONObject jsonObject=array.getJSONObject(0);
String ConditionId=jsonObject.getString("condition_id");
String ConditionName=jsonObject.getString("condition_name");
}
}
JSONArray jsonArray = new JSONArray("Your Data");
JSONArray tempArray ;
net.sf.json.JSONObject tempJson ;
for(int i = 0 ; i < jsonArray.length() ; i++)
{
tempArray = jsonArray.getJSONArray(i);
tempJson = tempArray.getJSONObject(0);
tempJson.get("condition_id");
....data So On
}
try this
String data = ""; //your json data string
JSONArray jarray = new JSONArray(data);
for(int i = 0;i < jarray.length(); i++) {
JSONArray jarry1 = jarray.getJSONArray(i);
for(int j = 0; j < jarry1.length(); j++) {
JSONObject jobj = jarry1.getJSONObject(0);
String ConditionId = jobj.getString("condition_id");
String ConditionName = jobj.getString("condition_name");
}
}
My JSON format is:
[
{
"change": 1.59,
"name": "ABC",
"price": 10.52,
"volume": 230
},
{
"change": -0.05,
"name": "DEF",
"price": 1.06,
"volume": 1040
},
{
"change": 0.01,
"name": "GHI",
"price": 37.17,
"volume": 542
}
]
I want to parse it and convert it into string. I am using this method for converting it:
JSONObject jsonObj = new JSONObject(jsonStr);
for (int i = 0; i < jsonObj.length(); i++)
{
String change = jsonObj.getString(TAG_CHANGE);
String name = jsonObj.getString(TAG_NAME);
String price = jsonObj.getString(TAG_PRICE);
String volume = jsonObj.getString(TAG_VOLUME);
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_CHANGE, change);
contact.put(TAG_NAME, name);
contact.put(TAG_PRICE, price);
contact.put(TAG_VOLUME, volume);
// adding contact to contact list
contactList.add(contact);
}
But I get an error:
/System.err(867): at org.json.JSON.typeMismatch(JSON.java:111)
How do I resolve this?
Please try it, It should work
JSONArray jsonObj = new JSONArray(jsonStr);
for (int i = 0; i < jsonObj.length(); i++) {
JSONObject c = jsonObj.getJSONObject(i);
String change = c.getString(TAG_CHANGE);
String name = c.getString(TAG_NAME);
String price = c.getString(TAG_PRICE);
String volume = c.getString(TAG_VOLUME);
HashMap < String, String > contact = new HashMap < String, String > ();
contact.put(TAG_CHANGE, change);
contact.put(TAG_NAME, name);
contact.put(TAG_PRICE, price);
contact.put(TAG_VOLUME, volume);
contactList.add(contact);
}
Try this..
You are getting response as JSONArray but you are doing it as JSONObject
[ // this is JSONArray
{ // this is JSONObject
Change this
JSONObject jsonObj = new JSONObject(jsonStr);
to
JSONArray jsonObj = new JSONArray(jsonStr);
You are parsing in wrong way. your json is starts with an array containing JsonObjects. This can be identified since square braces denote JsonArray while curly braces denote JsonObject. Start with:
JSONArray jsonArr = new JSONArray(jsonStr);
then iterate through each object, get JsonObject at each index and use getString method for each key to get values.