Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return specific portion of text from string 2

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
GB

Can any one help me with ms access 2003 vba code to pull

bonjour bonjour, bonjour. Bonjour

from the following string. Note the code will need to allow for any combination of words and any number of periods (.)

Code:
{"sentences":[{"trans":"bonjour bonjour, bonjour. ","orig":"good morning good morning, good morning.","translit":"","src_translit":""},{"trans":"Bonjour","orig":"good morning","translit":"","src_translit":""}],"src":"en","server_time":3}

I currently have

Code:
StrText= Split(strText, """trans"":""")(1)
StrText= Split(strText, """,""orig")(0)
StrText= Replace(strText, "quot;", Chr(39))

Which is no good as it always stops as the first Period (.) it encounters, hence this example would return the following

bonjour bonjour, bonjour.


 
I see this is your input string:[tt]
bonjour bonjour, bonjour. Bonjour[/tt]

What do you want to have as the output?

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
I do not know what data type will accept the "string" in the first code box. So to begin, I would want to know how to deal with getting programmatic access to the thinnngggggyyyyyy. I suppose there could be an array of strings or several arrays with different issues?

I made a BRIEF attempt to MANUALLY parse the thinnngggggyyyyyy ... So far I seem to see at least three sets of grouping delimiters, as well as the Colon (":") however they do not appear to be balanced.

I have previously liked doing puzzles. This one baffles me. I wish I could see some pattern.



MichaelRed


 
As far as splitting the text out, why not just use a few Replace functions to get rid of any other characters - periods, colons, etc, and then split on space as delimiter?

What else are you looking to get at? Sounds like you just want specific keywords perhaps?

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
We may keep guessing and asking, but if patriciaxxx does not answer any of the questions posted here, we may never be able to help. :-(

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Code:
[blue]Option Explicit

Private Sub CommandButton1_Click()
    Dim json As Object
    Dim sentence As Object
    Dim myOutput As String
    
    Set json = jsonDecode(TextBox1.Text) ' textbox1 is source of string to decode for this example
    
    For Each sentence In json.sentences
        myOutput = myOutput & sentence.trans
    Next
    
    MsgBox myOutput

End Sub

Private Function jsonDecode(jsonString As String)
    With CreateObject("ScriptControl")
        .Language = "JScript"
        Set jsonDecode = .Eval("(" + jsonString + ")")
    End With
End Function[/blue]
 
Very true, Andy. Have a pinky!

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
>We may keep guessing and asking, but if patriciaxxx does not answer any of the questions posted here, we may never be able to help

Oh, in this particular instance, I think we can ...
 
Wish I understood what motivates a person to ask a question here and then not bother returning for an answer. Patricia hasn't been on since she posted the question, nor did she mark it for notification, so what was the point?

Ah ... it's because she's cross-posting to other websites. And seems to have gone for a less than optimal solution.
 
Thanks for tracking that down. She did the same with at least one other thread around the same time.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
And I ended up learning something new. I think I had seen the term JSON mentinoed, but had no clue what it was. This prompted me to dig in at least a little to see what it was: JavaScript Object Notation, used in JavaScript and .NET according to this article:



"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Indeed, and that's why my solution works, because the jsonDecode function leverages JavaScript to completely decode the JSON string and return it as a COM object. Any JSON string (well, as long as it is syntactically correct).
 
(to be fair, there is a little bit of extra work you need to do to get access to the values in more complex json objects)
 
And the most obvious thing that you all failed to realise, is that this is an homework question.
(The question itself 'screamed' this).

You just ensured that Patricia will be slightly less likely to think for herself in future.

(Yes, you looked clever, but she became more likely to be thick forever).

I think that rather than get into 'madly obsessive, problem-solving mode' (which most of us here are - by default), we should be aware that our obsession can be abused by some lazy individuals; which doesn't help them whatsoever.

Patricia will no-doubt climb the management ladder (the only place for lazy, unethical individuals that sit on subordinates shoulders).

I will await Patricia's return with bated breathe, in order that she may prove me wrong in order that I may retract the above.

Darrylle ;-)

Never argue with an idiot, he'll bring you down to his level - then beat you with experience.
 
No, don't think it is. Patricia's been asking this sort of question here for the last couple of years or so, and it's pretty clear that it is not homework (here she's looking to talk to Google Translate) . But your analysis of her approach is pretty much spot on. She's admitted as much herself - back in Feb last year she told us

patriciaxxx said:
"I use a mix of code written and/or modified by me and/or others which I understand to varying degrees ranging from fair to good, sometimes not at all and rarely fully. If it works I tend to think (right or wrong) that it must be right, well anyway if it works and does what I wanted it to that’s where it stops.

which is why most of the time I generally only provide her with partial solutions or nudges in the right direction (as do others here), and challenge her to fill in the gaps. For example, here I only initially hinted as JSON in the hopes she (like kjv1611) might do some research for herself.
 
Strong,

Fair enough - I stand corrected.

I do prefer the 'nudging in the right direction' approach wherever possible though.
That's not to say: "Be obscure, provide minimal info", but I think that we can optimise what is provided in order that the questioner effectively resolves their own query - thus truly 'learning' the solution for future issues also.

I have to admit that sometimes, (due to work pressure) - I'm pretty much hoping for a copy 'n paste solution, however, you can bet your bottom dollar that I thoroughly examine and absorb such solutions before implementing them.

Darrylle ;-)

Never argue with an idiot, he'll bring you down to his level - then beat you with experience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top