Android parsing json file - android

Json string contained in String str:
{conf:{"quantity_uom":"l",price_uom:"euro",distance_uom:"km",consumption_uom:"km/l"}}
Code:
try{
if (str!=""){
this.json = new JSONObject(str);
this.quantityUom=this.json.getString("quantity_uom");
this.distanceUom=this.json.getString("distance_uom");
this.priceUom=this.json.getString("price_uom");
this.consumptionUom=this.json.getString("consumption_uom");
}
}catch (Exception e) {
throw e;
}
I have returned No value for quantity_uom. How I am doing wrong? String contain the json text.
Thank you so much.

try{
if (!str.equals("")){
Json obj = new JSONObject(str);
this.json = obj.getJSONObject("conf");
this.quantityUom=this.json.getString("quantity_uom");
this.distanceUom=this.json.getString("distance_uom");
this.priceUom=this.json.getString("price_uom");
this.consumptionUom=this.json.getString("consumption_uom");
}
}catch (Exception e) {
throw e;
}
You first have to get json object named 'conf' from string and from that json object get other values.

The JSON string is not correct.
Try this:
{"conf":{"quantity_uom":"l","price_uom":"euro","distance_uom":"km","consumption_uom":"km/l"}}

Related

Getting null when using getString with JSONObject class on Android

I have the following code:
JSONObject student2 = new JSONObject();
try {
student2.put("name", "NAME OF STUDENT2");
System.out.println(student2.get("name"));
System.out.println(student2.getString("name"));
} catch (JSONException e) {
e.printStackTrace();
}
I was just trying to create an JSONObject and extract the "name" value from it.
But the two "System.out.println" commands returns null. I expect the string "NAME OF STUDENT2" as a result.
What I was doing wrong?
try {
JSONObject jsonObject = new JSONObject("{\"name\":\"NAME OF STUDENT2\"}");
System.out.println(jsonObject.getString("name"));
}catch (JSONException err){
Log.d("Error", err.toString());
}

Sending json object to server in android

I am trying to send json to HTTP server with this JSON format:
{"deviceID":3852883413434, "depth":2,"location":{"xCord":46.232, "yCord":42.4421},"size":3}
and here is my code in android, but seems not working well.Maybe i need array for location or format my code.Any adivce could help thanks.
JSONObject postData = new JSONObject();
try {
postData.put("deviceID", unique_id);
postData.put("depth",((HoleSize)this.getApplication()).getDepth());
postData.put("xCord",((HoleSize)this.getApplication()).getLattitude());
postData.put("yCord",((HoleSize)this.getApplication()).getLongitude());
postData.put("size", ((HoleSize) this.getApplication()).getSize());
new SendData().execute("servername",postData.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Using the json response,
{"deviceID":3852883413434, "depth":2,"location":{"xCord":46.232,
"yCord":42.4421},"size":3}
you must get the JSON Object to get the value of depth and size (contained in the same object) and then get the JSON Object of location to get the values of xCord, yCord:
try {
JSONObject postData = new JSONObject(response);
String depth = postData.get("depth").toString();
String size = postData.getInt("size").toString();
JSONObject locationObject = new JSONObject(postData.get("location").toString());
String xCord = locationObject.get("xCord").toString();
String yCord = locationObject.get("yCord").toString();
} catch (JSONException e) {
e.printStackTrace();
}
applying this to your code:
try {
JSONObject postData = new JSONObject(response);
postData.put("deviceID", unique_id);
postData.put("depth",postData.get("depth").toString());
//postData.put("xCord", locationObject.get("xCord").toString());
postData.put("location", postData.get("location").toString());
postData.put("yCord",locationObject.get("yCord").toString());
postData.put("size", postData.getInt("size").toString());
new SendData().execute("servername",postData.toString());
} catch (JSONException e) {
e.printStackTrace();
}

How to create a JSON

I was create a JSON Object like below but i don't know how can fill parameters field with JSONStringer? And parameters field is a JSONArray or Array String?
{"name":"Katy", "parameters":["JAK","1999"], "Age":25}
Thanks for your help .
Try like below
String mParameters[] = { "JAK", "1999" };
JSONObject mJson = new JSONObject();
try {
mJson.put("name", "Katy");
JSONArray mJSONArray = new JSONArray(Arrays.asList(mParameters));
mJson.putOpt("parameters", mJSONArray);
mJson.put("Age", 25);
System.out.println("JSon::"+ mJson.toString());
} catch (JSONException e) {
e.printStackTrace();
}
The documentation for JSON will show you that an easy way to distinguish wheter your JSON string is an array is whether it starts with a [, so "parameters" in your example is a JSONArray.
Without knowing how you will be getting the data you want to put in your object, here is an example of how you would populate it (assuming you have an array of JAKS to insert).
JSONObject yourObject = new JSONObject();
String[] JAKS = {"1999", "2000", "2001"};
JSONArray paramaters = new JSONArray();
try {
yourObject.put("name", "Katy");
for (String JAK : JAKS) {
JSONObject yourParamater = new JSONObject();
yourParamater.put("JAK", JAK);
paramaters.put(yourParamater);
}
yourObject.put("parameters", paramaters);
yourObject.put("Age", 25);
} catch (JSONException e) {
e.printStackTrace();
}
Try this:
import org.json.JSONObject;
//other imports
//...
try {
//Create the JSON Object
JSONObject myObject = new JSONObject();
String parameters[] = new String[]{"JAK","1999"};
//use the method put to "fill" the values
myObject.put("name", "Katy");
myObject.put("parameter",(Object)parameters);
myObject.put("age", 25);
} catch (JSONException e) {
e.printStackTrace();
}
Try this way,hope this will help you to solve your problem.
try{
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","Katy");
jsonObject.put("parameters",new String[]{"JAK","1999"});
jsonObject.put("Age","25");
}catch (Throwable e){
e.printStackTrace();
}

Create Jsonstring - Android

Hi i want to create json string like
{"id":"12345","option-ids":["100"]}
i've tried like below
JSONObject object = new JSONObject();
try {
object.put("id","12314");
JsonArray jsonArray = new JsonArray();
jsonArray......
object.put("option-ids",""+jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
but i struck on create json array withou object name.
// First - create the json Object
JSONObject object = new JSONObject();
try {
// Add the id to the json
object.put("id", "12314");
// Create a json array
JSONArray jsonArray = new JSONArray();
// Put the values you want
jsonArray.put("1");
jsonArray.put("2");
object.put("option-ids",jsonArray.toString());
} catch (JSONException e) {
// Handle impossible error
e.printStackTrace();
}
To insert String in JSONArray,use put() method of JSONArray.
try below code.
JSONObject object = new JSONObject();
try {
object.put("id","12345");
JSONArray jsonArray = new JSONArray();
jsonArray.put("100"); //add here
object.put("option-ids",jsonArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
You should use JSONArray (from org.json) instead of JsonArray(from com.google.gson)

parse json without title object in android

i have this json : {"id":1,"name":"john smith"}
How i can parse it? i know i have to do this with this function:
public static String parseJSONResponse(String jsonResponse) {
String name = "";
JSONObject json;
try {
json = new JSONObject(jsonResponse);
JSONObject result = json.getJSONObject("**********");
name = result.getString("**********");
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
but i dont know what can i put in the areas incated with "****". please help me
i only want to fetch id and name values.
parse current json String as:
json = new JSONObject(jsonResponse);
// get name here
name = json.getString("name");
// get id here
id = json.getString("id");
because current json string contain only one jsonObject
use this method to parse your JSON String
public static String parseJSONResponse(String jsonResponse) {
try {
JSONObject json = new JSONObject(jsonResponse);
// get name & id here
String name = json.getString("name");
int id = json.getInt("id");
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
For more Refer this Link

Categories

Resources