JSON Conversation - android

I have a JSON which is returned from a Web Service Developed in PHP but I need help to converting this JSON to JSONArray in Android
{"Name":"John","Contact":"3331234567"}{"Name":"Doe","Contact":"3017654321"}{"Name":"Smith","Contact":"3001234765"}
How can i convert this String returned from URL to JSONArray

Thank you Guys for giving me an idea to fix this problem. I have figured it out bu conceding a string e.g: "n" while writing response on my web service now i can split this on my Android App.

I'd suggest to return it from the web service as a standard JSON Array, which means:
[{"Name":"John","Contact":"3331234567"},{"Name":"Doe","Contact":"3017654321"},{"Name":"Smith","Contact":"3001234765"}]
In this case you can parse it with any common json parser easily (gson, org.json, any other).
Returning it as you do it now produces invalid JSON - just put it to any online JSON validator and it will complain about this.

1. I suggested you to create json file in standard mode
like this:
{
"USER":[ {
"Name":"John",
"Contact":"3331234567"
}
{
"Name":"Doe",
"Contact":"3017654321"
}
{
"Name":"Smith",
"Contact":"3001234765"
}]
}
if you have not any field more , you can remove "USER":
2. You should use json parser library like GSON
**3.**use "pojo" site for create json model in java like http://www.jsonschema2pojo.org/

Related

Want to create multiple JSON Object in android

I have to pass JSON data in web services. Like below
"customerAccount": {
"payloadType": "KEYED",
"accountType": "CHECKING",
"cardDetails": {
"dataFormat": "PLAIN_TEXT",
"cardNumber": "5454545454545454",
"expiryDate": "1221",
"cvv": "123",
"issueNumber": "01"
}
}
Kindly guide me on how can I create JSON Object.
PS: Kindly not comment for JSONArray(). I have to use JSON Object, not JSONArray.
Do you mean some java json parse framework?
You can try fastjson or jackson.

Android Volley Post JSON Array with paramater and getting response in JSON Object

I am facing strange problem with Google Volley I hope some one help me out quickly. I want to send JSON array in parameter and server will give me response in JSON object. How can we achieve this?
e.g
I wanted to post this JSON array.
[
{
"name":"John",
"age":"30",
"cars":"6"
},
{
"name":"John",
"age":"30",
"car
}
]
and server will send response in JSON object format like this.
{
"status":"success",
"code":30,
}
Can someone explain to me how I can achieve this thing?. Moreover, my JSONarray consist of mobile contacts and size will be large.
You can use Map<Object, Object> where value be collection of json Element you want. You can make custom deserilizator with lib like "gson".
You should try: JSONArray arr = new JSONArray(JSON_STRING);

How to remove the special characters when getting the data using json in android?

I am working on android application. I need to get the data from PHP web service. The response which I am getting from PHP server is shown below:
String response = [{id:100,category:local,count1:58,count2:86},
{id:101,category:server/local,count1:18,count2:27},
{id:102,category:server & local,count1:19,count2:28}];
But I am getting the following message when I am trying to separate the data using JSON.
org.json.JSONException: Unterminated object at character
How to remove special characters and get the data using JSON array?
Strings should be under double quotes. Your json is not a valid response. So change your response from
{id:100,category:local,count1:58,count2:86}
to
{
"id":100,
"category":"local",
"count1":58,
"count2":86
}
Refer here
Valid JSON format
{
"Status":"OK",
"Error":{
"Code":0,
"Description":""
},
"Data":[]
}

Parsing JSON data from an XML web service in Android

I have an XML web service. I want to parse this XML and I want to store in an separate textviews. The following is an XML content, and I have finished getting it in a String variable.
{
"30_year_rate": 4.25,
"30_year_pi": 196.78,
"30_year_apr": 4.375,
"20_year_rate": 4.375,
"20_year_pi": 250.37,
"20_year_apr": 4.5,
"15_year_rate": 3.75,
"15_year_pi": 290.89,
"15_year_apr": 3.875,
"10_year_rate": 3.625,
"10_year_pi": 397.89,
"10_year_apr": 3.75,
"5_year_arm": 2.75,
"5_year_pi": 163.3,
"5_year_apr": 2.875,
"7_year_arm": 3,
"7_year_pi": 168.64,
"7_year_apr": 3.125,
"3_year_arm": 4,
"3_year_pi": 190.97,
"3_year_apr": 4.125,
"5_year_io_arm": "N/A",
"5_year_io_pi": "N/A",
"5_year_io_apr": "N/A",
"QuoteID": 1449,
"Licensed": "N"
}
How can I parse this data? I want to convert it to a JSON object and retrieve it, or any other reasonable approach.
If what you're getting back from the webservice is the string above, then you already have a JSON string. To create an object that can retrieve information from it, use something like JSONObject.
JSONObject object = new JSONObject(your_string_variable);
double thirtyYearRate = object.getDouble("30_year_rate");
String licensed = object.getString("Licensed");
etc.
You might (will) run into some issues where you try to pull a double from a JSON field that contains a string; i.e., the "N/A" fields above. You'll likely have to pull them out as strings and then try to parse doubles from them, and if the parsing throws an exception, you'll know it's a string.
Alternately, you could look into JSON binding with something like Jackson, which apparently runs on Android.
To parse JSON, you could use the built in JSONObject org.json Or Json-lib if you use an old version of android.
To parse XML, use XMLPullParser. A sample can be found here: Parsing XML Data

json parsing in android of web data

I have to parse this json data. The data begins with [ and ends with ]
How can we parse such json data? json data usually starts with {..[..]..}
Just create a JSONArray from your input. There is even a constructor taking a String as parameter. So, basicly you need to do something like this:
String input = .. //read your input
JSONArray arr = new JSONArray(input);
//work with the array as usual..
take result data as a JSONArray and not a JSONObject.
It depends on how you parse the data. Built in json or google json(gson) etc.
But normally you dont have to care about that it starts with square bracket.
Show me what the json array/object look like and I can give you an example.

Categories

Resources