Find a string and replace another string in Java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a text file which contains the follwing
{"sno":"1c8d1d7eaa32ff3f58a8822111276d5a","at":"app","tt":{"AppParam1":"AppParamValue1","AppParam2":"AppParamValue2"}},"c":{"dt":"Microsoft XDeviceEmulator","pn":"WP","pv":"8.0.9832.0"}
In java I want to find 'sno', 'pn' and 'pv' and replace the values of (what I mean is) currently the above text file has
"sno" has value "1c8d1d7eaa32ff3f58a8822111276d5a"
"pn" has value "WP"
"pv" has value "8.0.9832.0"
New values
"sno" has to be changed to "637829"
"pn" has to be changed to "XYZ"
"pv" has to be changed to "2.2.4.0"
Your help is much appreciated !
Thanks

That's a JSON object.
Convert that string to a JSON object, change the values and then convert it again to a string.
Here you can read about JSON objects in java: http://www.json.org/java/
And here an easy tuto on how to work with them: http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

You can convert it into JSON, make you changes and then convert it back to string.
Or if you really want to replace these values by yourself, you can use these lines :
yourstring = yourstring.replaceFirst("\"sno\":\"[^\"]+\"", "\"sno\":\"637829\"");
yourstring = yourstring.replaceFirst("\"pn\":\"[^\"]+\"", "\"pn\":\"XYZ\"");
yourstring = yourstring.replaceFirst("\"pv\":\"[^\"]+\"", "\"pv\":\"2.2.4.0\"");
Only if there are no way theses key can be found elsewhere in the string

Related

Reverse the JSON array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
String jsonString="{"name":"event_level_count","elements":[{"serial_number":"xxx12315","manufacturer_name":"xxx","count_level1":2004,"count_level2":1798,"count_level3":7},{"serial_number":"yyx01444","manufacturer_name":"xxx","count_level1":15,"count_level2":11,"count_level3":3}]}" ;
JSONObject output = new JSONObject(jsonString);
JSONArray docs = output.getJSONArray("elements");
System.out.println("Docs: "+docs);
Output Docs:
{"name":"event_level_count","elements":[{"count_level3":7,"count_level2":1798,"count_level1":2004,"manufacturer_name":"xxx","serial_number":"xxx12315"},{"count_level3":3,"count_level2":11,"count_level1":15,"manufacturer_name":"xxx","serial_number":"yyx01444"}]}
File file=new File("path \\ExportAsExcelfromJSON.csv");
String csv = CDL.toString(docs);
I need to reverse its as given string - jsonString.
JSON is simply a way to save or transfer data -- if you care about how it looks (e.g. how it's sorted), you are probably doing something wrong. If you want the array to be sorted in any way, do it once you have parsed the JSON back into actual data. Most programming languages (Java, for instance) come with easy-to-use tools to sort arrays.
As per Nizil's comment and this page, you can use the reverse(List<?> list) function from the Collections class in Java.

How to get data the given xml file corresponding to notes,totalqtypctmin,totalqtypctmax,mismatchthresholdpct,prodgrp,prodid,locgroup,geoid? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
<pko:POImport xmlns:xs="http://XMLSchema-instance"
xmlns:pko="hello"
xs:schemaLocation=" PlannedQuantityImport.xsd" tag="TAG" notes="These are my notes" totalqtypctmin="2" totalqtypctmax="3" mismatchthresholdpct="8.0">
<prodgrp id="100067">
<prodid>SYC87948427320</prodid>
<locgrp id="100067">
<geoid>30454</geoid>
<geoid>30982</geoid>
<quantity date="2010-12-11" sequence="1" prodid="87948427320">600</quantity>
</locgrp>
</prodgrp>
</pko:POImport>
I want to get the data corresponding to
tag, notes,totalqtypctmin,totalqtypctmax,mismatchthresholdpct,prodgrp,prodid,locgroup,geoid etc.
You will need XML parser to extract information from a XML using your desired xPath. Refer to this Best XML parser for Java.
I'd just use string matching.
inputStream = new BufferedReader(new FileReader("file.xml"));
while(inputStream.hasNext()){
String s=inputStream.next();
if(s.indexOf("totalqtypctmax")==-1)
continue;
String k = s.split("totalqtypctmax=\"")[1].split("\"")[0];
System.out.println("The value of totalqtypctmax is "+k);
}
Java isn't my first language, so pardon any silly mistakes.
EDIT: The only reason I'm using string matching instead of a standard XML parser, which is obviously recommended for more elaborate XML files, is because, in this case, your requirements seem very specific.
Their are various methods for parsing in java you could either use
DOM parsing
SAX parsing
These are included in the JDK by default
you can also use JDOM
Goto the following link to more info
http://www.mkyong.com/tutorials/java-xml-tutorials/

How to get key and value from a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a question about how to write a regex expression to get the key and value pairs from below string?
{"ReturnCode":"0","ErrorMsg":"success","EncryToken":"##xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
Could someone help me learn on what this can be done?
Thanks very much.
This one should suit your needs:
"([^"]+)"\s*:\s*"([^"]+)"
The key is in the first group, the value in the second one.
Visualization by Debuggex
You can use Java JSON or the Google Gson to parse this JSON string ...
as illustrated here - http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

Covert html to string in android [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a html tag
<html><body>You scored <b>192</b> points.</body></html>
I want to convert the html to string for this i use the code
String ni= Html.fromHtml((String) "<html><body>You scored <b>192</b> points.</body</html>").toString();
and i got the out put as
You scored 192 points.
But i want the out put as
You scored 192 points.
That is 192 should be bold. I want to print this result using a bluetooth printer. so can't use webview for view that.So is there any API to convert the HTML to String without change its format.
The problem is, that you're assigning the return-value of the Html.fromHtml()-method to a String-object. The method returns a Spanned-object, which is able to hold information like text-markup. By assigning it to a String, the object "forgets" these information.
This is possible, because the Spanned-interface implements the CharSequence-interface (therefor is a char-sequence) and String implements CharSequence.
This should however work:
Spanned ni = Html.fromHtml((String) "<html><body>You scored <b>192</b> points.</body</html>").toString();
In case your are trying to show that in TextView, you can use below code,
textView.setText(Html.fromHtml("YOUR HTML STRING"), TextView.BufferType.SPANNABLE);
Give it a try

Need to get string part between two / in a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to java need help
the main string i have is
eg.
"String1/string2/string3/string4/all_free.sdx"
"file1/file2/string3/string4/all_free.sdx"
the end result i need is to be able to isolate and get string3
i can indexof but not able to achieve it in few steps need brainy people help as i am new to JAVA
If you know it's the third item that you want to get, one simple approach could be using split method, like this:
String myString = "String1/string2/string3/string4/all_free.sdx";
String string3 = myString.split("/")[2];
The call of split("/") produces an array of strings like this:
{"String1", "string2", "string3", "string4", "all_free.sdx"}
Now you can apply the subscript operator to grab the element that you want.

Categories