Hi I have a JSON like:
{
"name" : "avg",
"code" : {"value":" import android
droid = android.Android()
droid.makeToast('Hello, Android!')",
"type":"PythonScript"
}
}
However, in value field(Python script for Android) I need a newline character between the 3 statements. Something like,
"value":"import android " + "\n" + "droid = android.Android()" + "\n" + "droid.makeToast('Hello, Android!')"
Is it possible to do this? Thanks in advance :)
Its better to configure your server to send you HTML-encoded value string. Afterwards, it would be easier to re-construct and show in TextView, for e.g.
Your expected Json response:
{
"name": "avg",
"code": {
"value": "import android<br />droid = android.Android()<br />droid.makeToast('Hello, Android!')",
"type": "PythonScript"
}
}
Once you read the value string:
String html_encoded_string = "import android<br />droid = android.Android()
<br />droid.makeToast('Hello, Android!')";
Then its easier to put it in TextView using html encoding:
yourTextView.setText(Html.fromHtml(html_encoded_string));
Related
I'm working on one application in which I want to fill the registration form and pass this data to API.I tried almost all solutions but not solved this issue.
I'm getting this type of data after filling the form
[
{Date of Birth of the Baby: 08/01/1997},
{Gender of the baby: male},
{Time of Birth of the Baby: 12.00 pm},
{Father's Name: Nnn},
{Mother's Name: Hbhh},
{Any Extra Details?: Bnn}
]
and I want this type of data
{
"Date of Birth of the Baby": "08/01/1997",
"Gender of the baby": "male",
"Time of Birth of the Baby": "12.00 pm",
"Father's Name": "Nnn",
"Mother's Name": "Hbhh",
"Any Extra Details?": "Bnn"
}
var fieldsData = [];
final myControllers = [];
mydynamicData() {
for (var i = 0; i <= widget.fieldData.length; i++) {
fieldsData.add({
widget.fieldData[i]['question'],
myControllers[i].text != null || myControllers[i].text != ""
? myControllers[i].text
: ""
});
}
print("fieldsData:${fieldsData}");
}
This is my method i know this issue occurred due to for loop but without for loop I'm not getting all fields data. so please help.
Import:
import 'dart:convert';
then you can use:
json.encode(fieldsData);
As you have a list of objects, it may be necessary to make a for in the list calling json.encode for each item, then encoding the result.
It seems that if you call
String text = "String<br>String";
Log.d(TAG, text);
it automatically parses the String to take two lines. The same goes for new line (\n) characters. That makes debugging more complicated. Is there a way to tell the logger to give me the exact String?
The arguments to the methods in Log class are Java strings, so escaping special characters is just like in Java. For example,
String text = "String\nString";
Log.d("TEST!!", text);
Will give you:
D/TEST!!﹕ String
String
while:
String text = "String\\nString";
Log.d("TEST!!", text);
will give you:
D/TEST!!﹕ String\nString
in the logcat.
As far as <BR>, I'm not seeing the same effect as you. Specifically,
String text = "String<br>String";
Log.d("TEST!!", text);
Produces:
D/TEST!!﹕ String<br>String
So I am unable to reproduce your actual problem. However, in general special characters in the Log strings are escaped just like any other Java strings. The logger is dumb and there's no settings to automatically escape special characters; you'll have to do this yourself for arbitrary strings. The Log methods just turn around and call println_native.
I use System.getProperty("line.separator")
ArrayList<String> txts = new ArrayList<String>();
txts.add("aoeuaeou");
txts.add("snhsnthsnth");
String msg = TextUtils.join(System.getProperty("line.separator"),txts);
Log.d(TAG, "Bla bla bla: "+ msg );
show in the log like
Bla bla bla: aoeuaeou
snhsnthsnth
At the end of the message, a trailing space seems to be needed.
Log.i("tag", "My message with a blank line following.\n ");
or
Log.i("tag", "Variable 1: " + v1 + " Variable 2: " + v2 + "\n ");
i have worked with json name value pair before works fine and great but below is my json object in which there is no name and only value. Can anyone tell is it possible to use this type of json in android if yes then how to extract values from this json object. or any related link will be great.
JSON OBJECT
{
"log_list": [
[
"21",
"doctorFisher",
"Pharmacy 1",
"patientfish",
"test",
"2014-08-25 05:58:18",
"record UCI tech FDNY icky shut rack soon sun TDK tell. ox it'll ohm URL did GCVO dash ugly dog did flood idiot fluff if if rid t-shirts didn't TechTV only chef doc scotch Rebekah an if lb tax scotch am ICN JCB JCB HGV JCB HGV in on pm tax UDC OK red uh HK ohm",
"<img id=\"prescription_image\" onClick=showPrescriptionDetails(\"3c59dc048e8850243be8079a5c74d079\") onmouseover=\"this.style.cursor='pointer'\" src=\"\" width=\"20\" height=\"20\">"
],
[
"20",
"doctortest12345",
"Pharmacy 1",
"testpatient12345",
"test",
"2014-08-25 03:57:32",
"she'd Urdu text scuff uno dad uno each ink why tough days ICO saved USCIS David rig though end FDIC UCI's for USCIS tend did for dog such vidi fly floor exited did DND hand bid GMD.",
"<img id=\"prescription_image\" onClick=showPrescriptionDetails(\"98f13708210194c475687be6106a3b84\") onmouseover=\"this.style.cursor='pointer'\" width=\"20\" height=\"20\">"
]
}
thanks in advance
Yes, that format would work and you can pretty easily use it.
Assuming you have a variable String json which contains that data:
JSONObject jsonObj = new JSONObject(json);
Now log_list would be parsed as a JSONArray like so:
JSONArray logJsonArray = jsonObj.getJSONArray("log_list");
Then you can iterate through that outer array very similar to how you would a normal array or ArrayList:
for(int outerArrayPos = 0; outerArrayPos < logJsonArray.length(); outerArrayPos++){
JSONArray innerArray = logJsonArray.getJSONArray(outerArrayPos);
for(int innerArrayPos = 0; innerArrayPos < innerArray.length(); innerArrayPos++){
String valueAtIndex = innerArray.getString(innerArrayPos); //innerArray
// Do whatever you want with the values here
}
}
That will parse your String, but figuring out how to actually use that data is going to be most of your struggle, so I hope the order in which the information is returned will always be the same for your sake.
If you can, I would definitely recommend using a JSONObject for the data that is currently contained in your inner array. e.g.
{
"log_list": [
{
"id": "21",
"name": "doctorFisher",
.....
}
]
}
I have a problem when I use Cordova 3.4 with Plugin SMS
.
If I send a normal message it works well, but if I send a URL like http://maps.google.com/maps?z=18&q=123123,12312313, where I set my location to send to my user, it doesn't work.
The plugin "stops working" and doesn't give any "success" or "error".
I think there is a problem of illegal characters.
This is my code:
var coordinate="http://maps.google.com/maps?z=18&q="+lat+","+long+"";
var dateSos=new Date();
var hourSos = dateSos.getHours();
var minSos = dateSos.getMinutes();
text=text+".Hours "+hourSos+":"+minSos+". LAt: "+lat+" Long: "+long+" Alt: "+alt+". "+coordinate+""; //This messagge doesn't work
//text="Hello world"; This message will send without error
text=$.trim(text);
var messageInfo = {
phoneNumber: number,
textMessage: text
};
sms.sendMessage(messageInfo, function(message) {
alert("success: " + message);
}, function(error) {
alert("code: " + error.code + ", message: " + error.message);
});
"lat" and "long" are variables of my coordinate.
Can someone help me?
I think I found the solution.
The problem seems to be the length of the character, sms must have a maximum length of 160 characters. If this length is exceeded, the plugin will not be any response.
I am trying to read JSON file from asset folder. But I get the following exception
org.json.JSONException: Expected literal value at character 550
I searched lot of stuff but didn't find anything relative. Here is my JSON file.
I find JSON object on 550 is "names": ["Santosh","Sandip","Arvind"],. I am trying to solve it but don't know what happens in my code.
Here is my code.
I also debug my code but when control goes on JSONObject jsonObject = new JSONObject(text); it throw exception and goes in first catch block.
Please give me any reference or hint to solve this problem.
Any help appreciated.
your JSON is invalid.your JSON should look like this
{
"resultCount": 3,
"SearchedTerm": "Wada Pav",
"results": [
{
"locationname": "Mahableshwar Hotel",
"locationid": "12345",
"locationaddress": "baner, Pune",
"dishrating": "4",
"dishname": "Wada Pav",
"dishid": "234",
"dishcategory": "Snacks",
"dishnotes": "Spicy Wada Pav",
"dishpreviewurl": "http://xxx.yyy.zzz/mahableshwar/1.jpg",
"dishtotalvotes": "9999",
"friendslistvoted": {
"friendscount": "3",
"names": [
"Santosh",
"Sandip",
"Arvind"
]
},
"dishimageurl": "http://xxx.yyy.zzz/mahableshwar/2.jpg",
"mylastrating": "4"
}
]
}
try using a JSON validator before using it (like JSLint).
I'm using following to get standard JSON format.
This one is better.
public static String convertStandardJSONString(String data_json) {
data_json = data_json.replaceAll("\\\\r\\\\n", "");
data_json = data_json.replace("\"{", "{");
data_json = data_json.replace("}\",", "},");
data_json = data_json.replace("}\"", "}");
return data_json;
}
I use jsoneditoronline online tool that works pretty good.
this works for me
public static String convertJSONString(String data) {
data = data.replace("\\", "");
return data; }