How to get this JSON format value in android? - android

How can I create a JSON with this format in Android?
I want to send this JSON in my SOAP Web Service Parameter
"{'\obj\':{\"MeterSrNo\":\"5\"},'\SPName\':'XXMFU_GETMobilityDetail'}"
SPName is my second parameter.

Use JSONObject
try {
JSONObject jsonObject = new JSONObject();
JSONObject obj = new JSONObject();
obj.put("MeterSrNo", "5");
jsonObject.put("obj", obj);
jsonObject.put("SPName", "XXMFU_GETMobilityDetail");
} catch (JSONException e) {
}

Related

get the token from header using volley in android

I am developing the android app and i want to get the token from header using volley which is in server json response and store it in sq-lite for maintaining the session of user,i search everywhere bt i am not getting how to retrieve the token from json,please help me about this issue
this is my json response
{ "token": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI5ODkzMjY3OTQ0IiwiYXVkIjoid2‌​ViIiwiZXhwIjoxNTA1OD‌​EwNTQ5LCJpYXQiOjE1MD‌​UyMDU3NDl9.cf9QGwp7z‌​LVSTuiunClkauTFEXe6j‌​tcNCxzqIntEtmWTDfLqA‌​D3nKiXIf-fg0vEV_74SO‌​cPVYxDcTUwCZqntVA" }
try this
JSONObject jsonObject = new JSONObject("your json string");
String token= jsonObject.optString("token");
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
String access_token= jsonObject.optString("access_token");
Log.v("####","hellow token " + access_token);
} catch (JSONException e) {
e.printStackTrace();
}
Try this

Parse JSON Android convert jsonObject

I have a problem with JSON
I get a json since https://proxyepn-test.epnbn.net/wsapi/epn
But when I want to display a single data eg "name".
The console displays:
Log
org.json.JSONException: No value for Name
org.json.JSONException: Value status at 0 of the type java.lang.String can not be converted to JSONObject
Can you help me ?
Thanks.
here is my code :
String test2 = test.execute(restURL).get().toString();
Log.i("result",test2);
JSONObject obj = new JSONObject(test2);
String data = obj.getString("data");
Log.i("testjson",data);
String pageName = obj.getJSONObject("data").getString("Name");
Log.i("testjsondata",pageName);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
Try below:
JSONObject obj = new JSONObject(test2);
JSONObject data = obj.getJSONObject("data");
Iterator<String> iterator = data.keys();
while(iterator.hasNext()){
String key = iterator.next();
String Name = data.getString(key);
}
JSONObject obj = new JSONObject(test2);
JSONObject data=obj.getJSONobject("data");
JSONObject ob1=obj.getJSONobject("1");
String pageName = ob1.getString("Name");
You have to parse your next level of JSONObject (labeled as "1","2","3".. from response).
It seems like issue in Json response structure you shared. why cann't it be array inside "data"?
Then you can easily read data as JSONArray with those objects as ("1","2","3"..) array item.
Else
Android JSON parsing of multiple JSONObjects inside JSONObject

How can I create Json with this format in android?

I want to send this serialize Json in Soap web service parameter to get data from server. I don know how to serialize Json like below.
I am doing this -
JSONObject jsonObject = new JSONObject();
try {
JSONObject obj = new JSONObject();
//obj.put("MeterSrNo", txtMeterSrMo.getText().toString());
obj.put("MeterSrNo", txtMeterSrMo.getText().toString());
jsonObject.put("obj", obj);
jsonObject.put("SPName", "XXMFU_GETMobilityDetail");
} catch (JSONException e) {
e.printStackTrace();
}
And its giving me below output-
{'obj':{"MeterSrNo":"5"},'SPName':'XXMFU_GETMobilityDetail'}
But i want below output. How can i achieve this?
"{'\obj\':{\"MeterSrNo\":\"5\"},'\SPName\':'XXMFU_GETMobilityDetail'}"
By using Gson Library you can do serialization and desertification.
Serialization
new Gson().toJson(object)
Deserialization
new Gson().fromJson(jsonString, MyClass.class);
Try following way,
JSONObject jsonObject = new JSONObject();
JSONObject jsonMeterObject = new JSONObject();
try {
jsonMeterObject.put("MeterSrNo","5");
jsonObject.put("obj",jsonMeterObject);
jsonObject.put("SPName","XXMFU_GETMobilityDetail");
} catch (JSONException e) {
e.printStackTrace();
}
Output
{
"obj":{
"MeterSrNo":"5"
},
"SPName":"XXMFU_GETMobilityDetail"
}

Json String created in Wrong Format

I have to send request body in Http request in below format:
{
"flag":false,
"Ids":["xyz","abc"]
}
I was trying like:
LinkedHashMap<String, Object> requestParamsMap = new LinkedHashMap<String, Object>();
requestParamsMap.put("flag",false);
ArrayList<String> IdList = new ArrayList<>();
IdList.add("xyz");
IdList.add("abc");
requestParamsMap.put("Ids", IdList.toString());
And then I was converting requestParamsMap to json string. But I am not getting request body in desired format.
I want to create a generalized method which can return me data in this type of format, so that I can use it throughout application.
Any help would be appreciated.. !!!
Do something like this:
JSONObject object=new JSONObject();
try {
object.put("flag","xyz");
JSONArray array=new JSONArray();
array.put("xyz");
array.put("abc");
object.put("Ids",array);
//added a log to see the output
Log.e("output",object.toString());
} catch (JSONException e) {
e.printStackTrace();
}
object.toString() should contain exactly the json you want in string format. You can use a loop to populate the contents of the jsonArray or JSONObject depending on the number of items you have
Its not that though at all, output you want is JSONArray inside JSONObject and JSONObject inside another JSONObject. So, you can create them seperately and then can put in together. as below.
CODE
try {
JSONObject parent = new JSONObject();
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
jsonArray.put("lv1");
jsonArray.put("lv2");
jsonObject.put("mk1", "mv1");
jsonObject.put("mk2", jsonArray);
parent.put("root", jsonObject);
Log.d("output", parent.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
OUTPUT
{
"root": {
"mk1": "mv1",
"mk2": [
"lv1",
"lv2"
]
}
}

How to parse json url using android?

I want parse json url,my json url contains following structures
{"content":{"item":[{"category":"xxx"},{"category":"yy"} ]}}
how to read this structure,anybody knows give example json parser for that.
Thanks
This code will help you to parse yours json.
String jsonStr = "{\"content\":{\"item\":[{\"category\":\"xxx\"},{\"category\":\"yy\"} ]}}";
try {
ArrayList<String> categories = new ArrayList<String>();
JSONObject obj = new JSONObject(jsonStr);
JSONObject content = obj.getJSONObject("content");
JSONArray array = content.getJSONArray("item");
for(int i = 0, count = array.length();i<count;i++)
{
JSONObject categoty = array.getJSONObject(i);
categories.add(categoty.getString("category"));
}
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject can do the parsing.
You need to use the org.json's package.
Example:
For an object:
JSONObject json = new JSONObject(json_string);
For an array:
JSONArray json = new JSONArray(json_string);

Categories

Resources