my json is
{[{"key1":"value1","key2":"valu2"},{“ key3":"value3","key4":"valu4”}]}
How to change the above text as follows. Thank you for helping my friends
{"travel": [{"key1":"value1","key2":"valu2"},{ key3":"value3","key4":"value4}]}
Your original String is not a valid JSON for two reasons:
1 we can see some invalid quotes
2 elements inside a json object must have keys.
so assuming that your String is correct to achieve what you need in java you can do the following:
String string="{[{\"key1\":\"value1\",\"key2\":\"valu2\"},{\" key3\":\"value3\",\"key4\":\"valu4\"}]}";
StringBuilder stringBuilder=new StringBuilder(string);
stringBuilder.insert(1,"\"travel\":");
String json=stringBuilder.toString();
You just insert your key after the first character. Hope this will help.
Related
First of all, I have gone through questions similar to the problem I am facing and those solutions are not working for me.
I have a TextView field on my Android app which is supposed to display multiple paragraphs i.e multiple new lines. I am getting this string from a database present in my online server as a JSON.
The text contains \n in it and I am expecting it to create new lines once it is received by the app. But it displays the whole text without any breaks along with "\n" character.
Below is the text present in my database.
First line. \nSecond line. \nThird line.
JSON string received by me inside the app.
{
"server_response": [{
"news_expand": "First line. \\nSecond line. \\nThird line."
}]
}
Code to extract string from JSON. I have left out the code to get get JSONArray and JSONObject for simplicity.
na_expand = gna_jo.getString("news_expand");
String extracted from the JSON. Got this by printing the na_expand string.
First line. \nSecond line. \nThird line.
Code to display the text in the TextView. Note the below 'na_expand' is an SparseArray present in a different activity hence the 'get(position)' code.
art_expand.setText(na_expand.get(position));
Below is the text I get on the emulator.
First line. \nSecond line. \nThird line.
What am I doing wrong here?
I think you should replace \n with \n in your string before setting test to your textview same below
b= b.replaceAll("\\n","\n");
So I found a workaround to the problem. As I was not sure where the issue was happening with \n, I modified my text present in the database to have a symbol other than \n. For eg: ~
First line.~Second line.~Third line.
You can use a website like this - https://www.gillmeister-software.com/online-tools/text/remove-line-breaks.aspx to replace the line breaks with any symbol you want.
Next, I used the StringSplitter class to break the string received in JSON and then again join it together with \n.
String joined;
String expand_temp = na_expand.get(position);
TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter('~');
splitter.setString(expand_temp);
StringBuilder stringBuilder = new StringBuilder();
for (String s_temp : splitter) {
stringBuilder.append(s_temp + "\n");
}
joined = stringBuilder.toString().trim();
This worked! I used this string in setText.
art_expand.setText(joined);
Try below code
myTextView.setText(Html.fromHtml("yourString with additional html tags"));
It will resolve all the html tags accordingly and effects of the tags will be reflected as well.
NOte: For devices greater than Nougat use below code
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
Hope that helps
The \ character is an escape character in JSON. So, when you get \\n, it actually means \n, not the newline character, which should have been just \n. So what you see is an expected behaviour. The JSON you get should have ideally been:
{
"server_response": [{
"news_expand": "First line. \nSecond line. \nThird line."
}]
}
Get your server to respond properly, otherwise you'll have to strip the unnecessary \.
Do you haveandroid:singleLine="true" on your TextView? If yes it will ignore the \n and will place the text in a single line.
You can just add replaceAll("\\n","\n") when you set value to your art_expand EditText. It should be:
art_expand.setText(na_expand.get(position).replaceAll("\\n","\n"));
In my application need to send String to a printer, with structured String
Like first line is bold and middle of the printing page.
After draw line and make three column
Item code item quantity
set items in each row
String line="_____________________________________________________________________"+"\n";
String cl= " <H1>Company Name(CLPPL) <H1>"+"\n";
print=line+Html.fromHtml(cl)+line;
I am trying Like above
I am using Html but it is not working
Please Help Me how i can make this kind of the String
Thanks IN Advance
Embedded html in Java code.Use StringBuilder class to append string one by one.
Use hint
StringBuilder stringBuilder=new StringBuilder();
stringBuilder_month.append("<html><body>");
stringBuilder_month.append("<font color="+"#413839"+"><h3 align="+"center"+"<b1>");
stringBuilder_month.append(" ");
stringBuilder_month.append("Your Heading</b1>");
stringBuilder_month.append("    ");
stringBuilder_month.append("</body></html>");
Add html tags as you required.You need to modify it according to your needs. This code will solve your problem
I got an xml with geo-coordinates which has several elements in one tag
<coordinates>37.481972,55.565380,0.000000</coordinates>
how do I deal with them?
or do I have to simply get this as a String and then search for "," to separate this 3 numbers?
Parse them as normal string .And later use the spilt function to convert them in to a string array
I am getting some strings from json. My string contains special characters like "æ" from Næstved an many more like "ø" from køkken. But When I set Text these strings to ant textview, I get my strings printed in unusual way.
Example: For køkken I get kø ;kken.
I think I need to encode or decode my string somewhere but where I don,t know.
Please help.
Thanks in advance
The displayed version of your string represents an HTML encoded entity. You might want to verify that it is not coming in this way in your JSON data, but in any case, to decode it you can use the StringEscapeUtils.unescapeHtml4 method from Apache Commons Lang:
final String escaped = "køkken";
System.out.println(StringEscapeUtils.unescapeHtml4(escaped));
Output:
køkken
Did you check out the Latin Coding for your characters? I know the Ash character can be coded with æ and will show up æ in the browser.
Here is the a list of codes
Hope this helps!
Hey, how can I read a value of a cookie?
Example:
String cs = CookieManager.getInstance().getCookie();
System.out.println("Cookies string: "+cs);
This will give me a string which has to be parsed with split on ';' and '='. Is there a "cookie string reader" or smth? Is there any other way of reading the value of only one particular cookie in the webview?
Thx!
Well, I suggest that you parse the string into an Array yourself. That would then be something along these lines in standard Java:
String[] x = Pattern.compile(";").split(CookieManager.getInstance().getCookie());
Now you have an Array of name - value pairs, which you can further parse and then store.